How do i print the results of self.grades using the repr method?

Hi ! can you please explain two things to me please, im dead stuck.

how do i print the results of self.grades using the repr method?
and
how the is_passing method can also be printed?

this is my 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:

  self.grades.append(grade)

def repr(self):

return self.name

return self.year    

class Grade:

minimum_passing = 65

def init(self, score):

self.score = score  

roger = Student(“Roger van der Weyden”, 10)

sandro = Student(“Sandro Botticelli”, 12)

pieter = Student(“Pieter Bruegel the Elder”, 8)

pieter.add_grade(Grade(100))

print(pieter.grades)

exercise link:
https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-classes/lessons/data-types/exercises/review

There should only be single return statement.

self.grades

is a list of Grade objects, each with a score attribute.

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

That’s one approach that returns a simple list of grades. Is that what you had in mind?

The is_passing can be tested on the average rather than on an individual grade. Just because we get a good grade doesn’t mean our average is higher than the minimum needed to pass.

Your Student class should have a get_average method. I don’t see one in your code.

Given an average, we can instantiate a new Grade object from that value and then call the is_passing() method on that object.

print(Grade(pieter.get_average()).is_passing())

yes!
how do i call the repr method properly to get the value to print. Do i have to turn it into a string first?

print() the instance.

print (pieter)

it doesnt work, what am i missing?? sorry im running on no sleep!!

class Student:
  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)

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

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

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

My bad. __repr__() must return a string. D’oh!

    return str([x.score for x in self.grades])

Will print as a string that looks like a list.

1 Like

Typically, we want our __repr__() method to give a full representation of our instance, meaning all attributes are composed into a string and returned.

So, with that in mind, let’s create some helper methods.

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

    def get_average(self):
        return sum(get_grades()) / len(self.grades)

    def __repr__(self):
        return f"Name: {self.name}\nYear: {self.year}\nGrade: {self.get_average()}"

Earlier we spoke of creating an instance of the average so we can check if it is a passing grade. We could add one more method to our Student class to do this…

    def is_passing(self):
        return Grade(self.get_average()).is_passing()

Now we can modify our __repr__() method to include that information.

    def __repr__(self):
        return f'''
Name: {self.name}
Year: {self.year}
Grade: {self.get_average()}
Pass? {self.is_passing()}
'''
2 Likes

amazing i got this perfectly now after 4 hours!!

2 Likes

perfect! i am working on this now and experimenting to understand it better. thats amazing thanks so much.

i will definitely ask you more questions later on if thats ok haha

1 Like

We put in the get_grades() method and didn’t really capitalize on it. How be we add one more item to the representation…

  def __repr__(self):
      return f'''
Name: {self.name}
Year: {self.year}
Grades: {self.get_grades()}
Grade: {self.get_average()}
Pass? {self.is_passing()}
'''
print (pieter)
Name: Pieter Bruegel
Year: 8
Grades: [100, 90, 88, 93]
Grade: 92.75
Pass? True

Remember before how we got caught up in the error about the list not being a string? Notice above how we get that string representation in our f-string, automatically without recasting.

i cant seem to figure out the is_passing and get grades
this is my code now:

class Student:
  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)        

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

  def get_average(self):
    return sum(get_grades()) / len(self.grades)

  def __repr__(self):
    return f'''
    Name: {self.name}
    Year: {self.year}
    Grades: {self.get_grades()}
    Grade: {self.get_average()}
    Pass: {self.is_passing()}
    '''

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

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

We cannot see the is_passing method in either of the Student class or the Grade class. Most crucial to the overall design. Mind, if you need to keep this on script with the exercise, we may be over-doing it. Otherwise, push on.

i keep getting these errors:

Traceback (most recent call last):
File “script.py”, line 36, in
print(pieter)
File “script.py”, line 24, in repr
‘’’
File “script.py”, line 15, in get_average
return sum(get_grades()) / len(self.grades)
NameError: name ‘get_grades’ is not defined

And the error message is correct. It is telling us there is no get_grades function in global scope. We are referring to a method of a currently scoped context object. Easy mistake to make but one to learn from. self is the context object.

... sum(self.get_grades()) ...

i just want to try and understand the extra exercises completely but i cant seem to

is this how it should look like?

class Student:

def init(self, name, year):

self.name = name

self.year = year

self.grades = []

def is_passing(self):

    return Grade(self.get_average()).is_passing()

def add_grade(self, grade):

if type(grade) == Grade:

  self.grades.append(grade)        

def get_grades(self):

return [x.score for x in self.grades]

def get_average(self):

return sum(self.get_grades()) / len(self.grades)

def repr(self):

return f'''

Name: {self.name}

Year: {self.year}

Grades: {self.get_grades()}

Grade: {self.get_average()}

Pass: {self.is_passing()}

'''

class Grade:

minimum_passing = 65

def init(self, score):

self.score = score  

roger = Student(“Roger van der Weyden”, 10)

sandro = Student(“Sandro Botticelli”, 12)

pieter = Student(“Pieter Bruegel the Elder”, 8)

pieter.add_grade(Grade(100))

roger.add_grade(Grade(60))

print(pieter)

You’re not formatting your code. We cannot tell. Please format the code while you still can.


Bear in mind, the code expressed here is not meant for use as a solution. It is another avenue entirely. Don’t be led to believe this is in any way meant to help you pass an exercise. It goes way beyond that. Perhaps we’ve gotten ahead of the horse?

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

  def is_passing(self):
        return Grade(self.get_average()).is_passing()
        
  def add_grade(self, grade):
    if type(grade) == Grade:
      self.grades.append(grade)        

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

  def get_average(self):
    return sum(self.get_grades()) / len(self.grades)

  def __repr__(self):
    return f'''
    Name: {self.name}
    Year: {self.year}
    Grades: {self.get_grades()}
    Grade: {self.get_average()}
    Pass: {self.is_passing()}
    '''

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

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

The Grade class still doesn’t have an is_passing method.

yes totally understand, but i feel like i need to get this to fully understand CLASSES

Start by finishing with an is_passing method on the Grade class.