How to get a list inastance variable from a method in readable format?

Link to exercise:

How can I get pieter.grades to print as a list? I’ve thought of using repr() but that would affect how the method is displayed every time and I just want the list of grades.

  def __init__(self, name, year): = name
    self.year = year
    self.grades = []

  def add_grade(self, grade):
    if type(grade) == Grade:

roger = Student("Roger van der Weyden", 10)
sandro = Student("Sandro Botticelli", 12)
pieter = Student("Pieter Bruegel the Elder", 8)

class Grade:
  minimum_passing = 65
  def __init__(self,score):
    self.score = score

pietergrades = pieter.grades


Grades are objects with a score attribute. You can write a method to extract the scores to a list, or write a for loop to iterate the grades attribute.

grades = [x.score for x in pieter.grades]

Now it will be a simple list of grades that can be summed and an average computed.

Ideally all of this will be done inside the class.

1 Like