Classes Lesson: How to add Grade type objects from a list together as integers

From the Introduction to Classes Review: Link to exercise

Hi all! This is my first post here. I’ve been stumped on a question for 3 days now and haven’t found any post to help me advance (though this one came close).

I am at the end of the Introduction to Classes lesson. I’m trying to get the average of the list of scores for the object named ‘Pieter’. I’ve managed to represent the objects in the list pieter.grades as strings when I print. However, whenever I tried to add up the items in the list, I’m told objects of type Grades can’t be added. The only way I successfully manage to add them is to convert to a string and then to an integer.

Is there a way to do this “correctly”? I’ve read about adding an add method to define how objects should be added, but I couldn’t successfully get my values to add when I added such a method.

class Student: def __init__(self, name, year): = name self.year = year self.grades = [] def __repr__(self): return str(self) def add_grade(self, grade): self.grade = grade if type(grade) == Grade: self.grades.append(grade) return self.grades def get_average(self): self.count_grades = len(self.grades) self.sum_grades = 0 for grade in self.grades: self.sum_grades += int(str(grade)) return self.sum_grades / self.count_grades class Grade: minimum_passing = 65 def __init__(self, grade): self.grade = grade def __repr__ (self): return str(self.grade) def is_passing(self): if self.grade >= Grade.minimum_passing: return f"The grade {self.grade} is a passing score." else: return f"The grade {self.grade} is not a passing score." pieter = Student("Pieter Bruegel the Elder", 8) grade_p = Grade(65) grade_1 = Grade(90) grade_2 = Grade(51) grade_3 = Grade(70) pieter.add_grade(grade_p) pieter.add_grade(grade_1) pieter.add_grade(grade_2) pieter.add_grade(grade_3) print(pieter.grades) print(pieter.get_average())

Here is what I tried adding to the Grades class in order to show how objects should be added, but it had no effect.

def __add__ (self, other): return self.grade + other.grade

You can’t sum the grade instances/objects, that is true. However the grade class has grade property, this property is an integer, which is what we want to use:

    for grade in self.grades:
      self.sum_grades += grade.grade
1 Like

Thank you for your help! I don’t fully understand why this works, but I appreciate the quick answer. I’ll have to think on this some more. :slight_smile:

why not? The grade class has a grade property:

class Grade:
  minimum_passing = 65

  def __init__(self, grade):
    self.grade = grade  

although I would name the property score to be less confusing:

class Grade:
  minimum_passing = 65

  def __init__(self, score):
    self.score = score

this means an instance/object of grade:

grade_p = Grade(65)

has score instance property we can use:


which is also what I do in the loop

the self.grades property of Student is a list with grade objects, when looping over list, we get the objects, of which we can access properties