? 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.
Hi,
I am really struggling with this part. Unable to execute the add_attendance() method.
Please help.
Thanks.
If you want the full solution look at the bottom.
To validate the date I imported datetime and used strptime()
from datetime import datetime
don’t forget to add this in the classes’ __init__
self.attendance = {}
and to validate the attendance.values() as bool
type(attendance[i]) == bool
Solution:
This might not be the elegant solution but it seemed to work for all the tests I threw at it
def add_attendance(self, atten_date):
for i in atten_date:
try:
date = datetime.strptime(i, '%Y-%m-%d')
if date and type(atten_date[i]) == bool:
self.attendance[i] = atten_date[i]
else:
print('Need a bool value. Example: {Date: True}')
except ValueError or UnboundLocalError:
print('Date must be in \'2020-07-30\', year-month-day')
Hi all. Having trouble with this review exercise.
Below is the screenshot of the solution:
Essentially, I’m not understanding HOW lines 7-9 interact with lines 11-12 and again how line 20 brings it all together. A “code to English” translation would help if possible. My questions are below:
1) We’re saying here, that we want to add a new method. Cool. But then, are we checking to see if the type of the grade matches Grade? I get checking the minimum passing grade, but we need to confirm the type? Why? I don’t get why this is needed.
def add_grade(self, grade):
if type(grade) is Grade:
self.grades.append(grade)
2) Here, are we setting the minimum passing grade to compare to in the method above?
class Grade:
minimum_passing = 65
3) My understanding is that we want to add a grade to Pieter’s .grades attribute. Cool. But why do we need to do what’s done below?
pieter.add_grade(Grade(100))
I would think that you just need to do what I’ve written below. I don’t understand the context of why or what “Grade” does in that code.
pieter.add_grade(100)
Thank you in advance. I’m aware that my question is all over the place, but any help will really point me in the right direction. Have just been out of it for a little while.
by passing an instance of Grade class to add_grade method, we can access the minimum_passing and score properties.
This really starts to become useful when we implement other methods for student, for example a method which checks the student passed all exams.
at the moment, Grade doesn’t do much/doesn’t add much value. But there is potential to wrap the score in a class.
Oh. I see. That actually helps a lot!
Thank you stetim94, appreciate it.
can you say correct code for “attendance” in these exercise?
You want the solution to the exercise? Just google, should be easy enough to find. Use to the forum to get help, much better then the solution (there is a massive difference between the two)
I am a bit confused on why we dont initialize grade with self in the .add_grade method.
Basically why is this preferable
def add_grade(self, grade):
if type(grade) is Grade:
self.grades.append(grade)
over
def add_grade(self, grade):
self.grade = grade
if type(self.grade) is Grade:
self.grades.append(grade)
what is the difference between the two?
Thanks!