11 posts were split to a new 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>?)
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’
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.
? 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)
Recall that grade
is a Grade
object with a score attribute.
total += grade.score
yes that is what i have above thanks for explaining
total = total + grade.score
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.
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 = []
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?
‘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.
My assumption was they are trying to use what you learned in the add_grade() method with validating types and apply it to a dictionary.
>>>print(name.attendance)
{‘2020-07-30’: True}
I made add_attendance() to validate the inputs.
>>>datajumper.add_attendance({‘2020-07-30’: True})
>>>print(datajumper.attendance)
{‘2020-07-30’: True}>>>datajumper.add_attendance({‘2020-07-30’: 4})
>>>print(datajumper.attendance)
{}>>>datajumper.add_attendance({‘cool’: True})
>>>print(datajumper.attendance)
{}
Hope this helps. If you want my add_attendance() method, let me know.