In my Student subclass below, I am calculating average GPA from manually inputted grades stored in the self.courses dict attribute as {course:grade}.
The user should be able to enter in the console >>>print(fred.gpa),given fred is a proper Student instance with grades in self.courses, and should get 3.8 (for example) printed to the console.
However, 3.8 does not print to console, rather <bound method Student.gpa of <college.Student object at 0x7ff55e122ad0>>
I understand that this is the result of printing a function, but I want to print just a number using just print(fred.gpa) and not fred.gpa()
Does this mean I have to convert the output of gpa.Student into a string?
Here is my code for ref:
def __init__(self, name, cid, email):
self.courses = {}
super().__init__(name, cid, email)
def add_course(self, course):
if course in self.courses:
# duplicate courses not allowed
raise ValueError
print("student is already registered for this course")
else:
self.courses.update({course:0})
return self
def update_grade(self, course, grade):
if course not in self.courses:
# student must be registered in class to receive grade
raise ValueError
print("student is not registered for this course")
else:
self.courses[course] = float(grade)
return self
def gpa(self):
grades = list(self.courses.values())
totGPA = sum(grades)/len(grades)
return str(totGPA)