sorry for a stupid question maybe but I am very very new to python and created one of my first classes. But its giving error and I am kinda clueless. Here is the code
class Student:
studentCount = 0
averageGPA = 0
def __init__(self, studentID, studentName, studentGPA):
self.studentID = studentID
self.studentName = studentName
self.studentGPA = studentGPA
Student.studentCount += 1
Student.averageGPA += 3
def displayCount(self):
print ("Total Students %d" % Student.studentCount)
def displayStudent(self):
print ("ID : ", self.studentID, ", Name : ", self.studentName, ", GPA : ", self.studentGPA)
def averageGPA(self):
print("Average GPA is : %d" % Student.averageGPA/Student.studentCount)
def main():
student1 = Student("54466","Zara", 3)
student2 = Student("48887","Manni", 4)
student3 = Student("41187","Sam", 3)
student1.displayStudent()
student2.displayStudent()
print ("Total Students %d" % Student.studentCount)
print ("Student's average GPA %d" % Student.averageGPA)
main()
and the error it is giving is
Traceback (most recent call last):
File "Z:/CS 120/lab7_2.py", line 29, in <module>
main()
File "Z:/CS 120/lab7_2.py", line 21, in main
student1 = Student("54466","Zara", 3)
File "Z:/CS 120/lab7_2.py", line 10, in __init__
Student.averageGPA += 3
TypeError: unsupported operand type(s) for +=: 'function' and 'int'
Please help me out with this problem. Again sorry if this question sounds stupid as I m very new!!!
you are passing the gpa as a string : is there a reason to do that. It would be better if you did this :
As others have said also that AverageGPA is the name of a method, so you need another name for the class attribute.
ALso you will probably find that your average will come out wrong if you are using Python 2.7