Python Classes Review - Further Practice Discussion

But the self.grades variable is just a list with 2 Grade objects in it.


[<__main__.Grade object at 0x7f150a301668>, <__main__.Grade object at 0x7f150a3016a0>]

And if I try to do any kind of calculation on these Grade objects I get an error

S.grades[0] + S.grades[1]

  File "", line 25, in <module>
    S.grades[0] + S.grades[1]
TypeError: unsupported operand type(s) for +: 'Grade' and 'Grade'

I get the same error if I try to do the calculation in the get_average method in the Student Class.
Could you maybe provide an example of how I could preform calculations on these objects?

1 Like

Hello, @fox3773.

grades is a list containing Grade objects as you’ve mentioned. You need to access the property of the Grade object that contains the value. You are almost there with S.grades[0]. That is the first grade object. How do you access its property?

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

Thanks @midlindner,
This really did help, but it still took me a long time to figure out I was looking for S.grades[0].score
I am definitely going to need some more time to get a handle on classes.

1 Like

Since we are instantiating a Grade class within the argument of the add_grade() method, it doesn’t get assigned as a referable instance except by accessing the grades instance variable (the list of grades).

  def get_grades(self):
    return [x.score for x in self.grades]
grades = pieter.get_grades()
print (grades[0])    # 100

It’s also nice to incorporate the minimum_passing attribute of Grade into the Student class, to check if a student will pass given their average:

  def will_pass(self):
    return self.get_average() >= Grade.minimum_passing

Anyway, I finally understand this lesson completely. Thanks for the support here on the forums everyone!


4 posts were split to a new topic: Can’t make get_average to work

2 posts were split to a new topic: Why did this one need return?

A post was merged into an existing topic: How can I access Pieter’s grade after it has been added?

7 posts were split to a new topic: What do we actually need to do in the last additional “challenge”

2 posts were split to a new topic: To use datetime objects as the key value for the dictionary

6 posts were split to a new topic: Explain the meaning of type(grade)==Grade?

28 posts were split to a new topic: How do i print the results of self.grades using the repr method?

changing grade to grade.score worked for me.

print(grade) gives you <main.Grade object at 0x7feebe938b38> or something like that

print(grade.score) gives you a int

the int you can append to self.grades

You’ll want to append Grade objects (instances of the Grade class) not just the score. If all we want to append is the score, there’d be no point in creating Grade objects.

1 Like

4 posts were split to a new topic: How did you make it a subclass?

3 posts were split to a new topic: Does the below work for the get average question?

5 posts were split to a new topic: What’s going wrong despite using “grade.score” to append “self.grades”

6 posts were split to a new topic: What is the mean of code if type(grade) == Grade

This is my first post here, so here goes…

I also fought with the solution to the average score section of this Review. I read all the posts here and came up with this solution. I’m still not feeling like I fully understand this lesson, but I am learning a lot, and I am happy to get this working!

Under “class Student:”

def get_average(self):
    #var. to hold scores as added
    temp = 0
    #var. to get length of how many scores are in grades
    avgl = len(self.grades)
    #var. to limit number of loops for while loop
    counter = 0
    while counter < avgl:
        #adds each score to the total. The counter var will advance and allow access to each list item.
        temp += self.grades[counter].score
        #advances count for loop
        counter += 1
    #calculate average of scores    
    avg = temp / avgl
    return avg



If I missed the point of the exercise, any input is welcome please!

Given the amount of iterations is know, I would use a for loop.