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’
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.
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?
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 = []
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?