Need help for Classes: review

Hi guys, when I doing this review from step 1 to 8, I don’t get the idea of Creating the class Grade. As I understand it really doesn’t help anything when I keep this line of code:

if type(grade) == Grade:

Here my full review code:

class Student:
  def __init__(self, name, year):
    self.name = name
    self.year = year
    self.grades = []
  def add_grade(self, grade):
    **if type(grade) == Grade:**         #Why you have to check it a type of Grade?
      self.grades.append(grade) 
  def print_grades(self):
    for grade in self.grades:
        print(grade.score)
    
    

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))


print(pieter.print_grades())

we have to type check, otherwise we could just pass integers:

pieter.add_grade(100)

or strings:

pieter.add_grade("100")
2 Likes

Can you explain more about what exactly class Grade do?
Thank you.

Classes come alive more once you code base starts to grow.

But you first need to learn about them. In this case, the Grade class can help us to specify some behavior, we could add an is_passing method to check if the grade meets the minimum.

1 Like