How can I access Pieter's grade after it has been added?

Forgive me for not scrolling up to find a link to the lesson. As I recall this exercise has two classes, Grade and Student. Please correct me if I’m wrong. We’re focused on the Grade class, which I’m pretty sure has a score attribute.

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


>>> a_grade = Grade(99)
>>> a_grade.score
99
>>> 

A Student would necessarily handle more than one grade (accumulate) and thus have a reference object to ‘contain’ them. One would guess a student class might look like,

class Student:
    def __init__(self):
        self.grades = []
    def add_grade(self, grade):
        self.grades.append(grade)

Knowing that every grade entry is a Grade object, we need to keep that in mind when polling the grades attribute.

    def get_grades(self):
        return [x.score for x in self.grades]

Under this dictate we get,

>>> peter = Student()
>>> peter.add_grade(Grade(99))
>>> peter.add_grade(Grade(89))
>>> peter.add_grade(Grade(95))
>>> peter.add_grade(Grade(85))
>>> peter.add_grade(Grade(94))
>>> peter.get_grades()
[99, 89, 95, 85, 94]
2 Likes