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

Link to exercise: https://www.codecademy.com/courses/learn-python-3/lessons/data-types/exercises/review

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):
    self.name = name
    self.year = year
    self.grades = []




  def add_grade(self, grade):
    if type(grade) == Grade:
      self.grades.append(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

pieter.add_grade(Grade(100))
pietergrades = pieter.grades

print(pietergrades)
print(list(pietergrades))
print(pieter.name)
print(pieter.year)

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