FAQ: Learn Python: Classes - Review

This community-built FAQ covers the “Review” exercise from the lesson “Learn Python: Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Review

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

11 posts were split to a new topic: Python Classes Bonus Challenge Discussion

A post was split to a new topic: How can I test class code?

A post was merged into an existing topic: Python Classes Bonus Challenge Discussion

34 posts were split to a new topic: Class Attribute Representationt (or why does it print this <main.Grade object at 0x7f2b2baf4be0>?)

6 posts were merged into an existing topic: Python Classes Bonus Challenge Discussion

10 posts were split to a new topic: How can I access Pieter’s grade after it has been added?

why ami i getting this typeerror unsupported on this here is the code. I want to print out Pieters grade average:

class Student:
  def __init__(self,name,year):
    self.name = name
    self.year = year
    self.grades = []
    self.attedence = {}
  def add_grade(self,grade):
    if type(grade) == Grade:
      self.grades.append(grade)
      
  def get_average(self):
    total=0
    for grade in self.grades:
      total = total + grade
    average = total/len(self.grades)
    return average



class Grade:
  minimum_passing = 65
  def __init__(self,score):
    self.score = score
  def is_passing(self):
    if self.score > self.minimum_passing:
      return True
  

    
  
roger = Student('Roger van der Weyden',10)
sandro = Student('Sandro Botticelli',12)
pieter = Student('Pieter Bruegel the Elder',8)
pieter.add_grade(Grade(100))
average=pieter.get_average()
print(average)


  

and I get the following:
Traceback (most recent call last):
File “script.py”, line 35, in
average=pieter.get_average()
File “script.py”, line 14, in get_average
total = total + grade
TypeError: unsupported operand type(s) for +: ‘int’ and ‘Grade’

1 Like

The error helps? Python tells you the types (int and instance of Grade) class are not compatible to give you a sum.

Each grade has a score.

1 Like

? I am adding elements from the self.grades list and those should be integers

self.grades is a list containing instances of Grade class (you could verify this by simply printing the elements from the list), which are absolute not integers.

ok thank you, looking , apparently I am missing an understanding somewhere…

oh ok i would have to reference grade.score in the for loop. thank you

class Student:
  def __init__(self,name,year):
    self.name = name
    self.year = year
    self.grades = []
    self.attedence = {}
  def add_grade(self,grade):
    if type(grade) == Grade:
      self.grades.append(grade)
      
  def get_average(self):
    total=0
    for grade in self.grades:
      **total = total + grade.score**
    average = total/len(self.grades)
    return average



class Grade:
  minimum_passing = 65
  def __init__(self,score):
    self.score = score
  def is_passing(self):
    if self.score > self.minimum_passing:
      return True
  

    
  
roger = Student('Roger van der Weyden',10)
sandro = Student('Sandro Botticelli',12)
pieter = Student('Pieter Bruegel the Elder',8)
pieter.add_grade(Grade(100))
average=pieter.get_average()
print(average)


  

1 Like

Recall that grade is a Grade object with a score attribute.

total += grade.score

yes that is what i have above thanks for explaining :grinning:
total = total + grade.score

1 Like

Hi all,

Thank you for reading this. I’ve come across a peculiar situation involving 'double quote and ‘single quote’ use and classes. I’m going to specify how I arrived at this issue and in doing so, also explain the issue.

In this exercise, I first created the Student class:

 class Student():
      def __init__(self, name, year):
           Student.name = name
           Student.year = year

And then I created the objects as follows (please not the use of single quotes):

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

I then realized that, whether I tried to print roger.name or sandro.name or pieter.name, they all printed “Pieter Bruegel the Elder.” Essentially, it seems like the objects followed the last instance in which a Student class was “objectified.”

Changing the single quotes to double quotes fixes the issue. My question is, why is this happening?

Thank you again.

1 Like

Consider…

>>> class Student():
      def __init__(self, name, year):
           Student.name = name
           Student.year = year

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

The lesson isn’t accepting my code. I didn’t get an error, but the lesson keeps on asking me if I created an empty list and saved it as self.grades. Here is my code for reference:

class Student:
  def __init__(self, name, year):
    self.name = name
    self.year = year
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
    self.grades = []
1 Like

Think of it, for a second. Would a Grade object have a ‘grades’ attribute? Wouldn’t that be kind of fourth dimension; a circular reference, Möbius strip sort of thing?

The only attribute a Grade class needs is an individual score value. The object is a granule, not a precipitate. The only method to attach to this object would be a ‘does_pass’ that says it earns a star. Otherwise this is a rather static class that pumps out grades, not collections of grades.


Here’s a thought… What if each Student had a ‘grades’ attribute?

1 Like

‘Here, is my solution’

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

        
    #adding grades to empty list
    def add_grade(self, grade):
        if type(grade) is Grade:
            self.grades.append(grade)
    
    # fetching grades from list
    def get_grades(self):
        return [g.score for g in self.grades]
            
    
    #calculating average of all grades            
    def get_average(self):
        return sum(self.get_grades())/len(self.grades)     
    
    #print all info
    def __repr__(self):
        
        return  f"""
                Name: {self.name}
                Year: {self.year}
                Grades: {self.get_grades()}
                Average: {self.get_average()}
                 """

   
class Grade:

    minimum_passing = 65
    def __init__(self, score):
        self.score = score
  
    def is_passing(self):
    
        if self.score > minimum_passing:
            return True
        return False
    
roger = Student("Roger van der Weyden", 10)
sandro = Student("Sandro Botticelli", 12)
pieter = Student("Pieter Bruegel the Elder", 8)

pieter.add_grade(Grade(100))
pieter.add_grade(Grade(30))
pieter.add_grade(Grade(89))

sandro.add_grade(Grade(59))
sandro.add_grade(Grade(90))
sandro.add_grade(Grade(30))


roger.add_grade(Grade(100))
roger.add_grade(Grade(100))
roger.add_grade(Grade(100))

k = pieter.get_grades()
print(k)

l = pieter.get_average()
print(l)

print(repr(pieter))
print(repr(sandro))
print((repr(roger)))

I am bit stuck here in understanding, what exactly is expected??

• Add an instance variable to Student that is a dictionary called .attendance, with dates as keys and booleans as values

Do I have to :

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

attendance = { 12-3-2020: 'Yes', 13-3-2020: 'No'}

How should be the outputs ?

Thanks in advance.
:slight_smile:

1 Like