After writing the is_passing method, how do we use it on one of the students we made to see if they’re passing?
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)
class Grade:
minimum_passing = 65
def __init__(self, score):
self.score = score
def is_passing(self):
if self.score >= Grade.minimum_passing:
return True
else:
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))
x = Grade(70)
print(x.is_passing())
print(pieter.grades.is_passing())
print(x.is_passing()) works fine.
The line after it doesn’t work, and I’m not sure how to write to check if Pieter is passing. He has a Grade of 100, so shouldn’t there be a way to check that?
Would checking each grade in turn be any indication of having a passing grade? Not really.
There is a method missing in the Student class…
Write a Student method get_average() that returns the student’s average score.
It is in this way that we can obtain a single grade to test.
average = pieter.get_average()
There is no way to tie this to Grade as an instance so we create a transient instance using that average and then call the is_passing() method on that instance.
After I added a grade to add_grade, I actually want to fetch the content of self.grades list. I tried it with simply print(pieter.grades) but it just print the object id
Ok, I came up with this solution, but I think it is not very pythonic.
Can you please review and give me some recommendations for cleaner code. Is there a way to write the add_grade() and is_passing() calls at the end in one line?
Thanks!
class Student:
def __init__(self, name, year, day):
self.name = name
self.year = year
self.day = day
self.grades = []
self.attendance ={'Monday':True,
'Tuesday':False,
'Wednesday':False,
'Thursday':True,
'Friday':True}
def add_grade(self, grade):
if type(grade) == Grade:
self.grades.append(grade)
def get_average(self):
x = 0
for student in self.grades:
x += student.score
return x/(len(self.grades))
def availability(self):
for key, val in self.attendance.items():
if key == self.day:
return val
def __repr__(self):
return f'Name: {self.name}\nYear: {self.year}\nGrade:{self.get_average()}\Attendance: {self.availability()}'
pieter = Student('Pieter Bruegel the Elder', 8, 'Wednesday')
class Grade:
minimum_passing = 65
def __init__(self, score):
self.score = score
def is_passing(self):
if self.score > Grade.minimum_passing:
return True
else:
return False
pieter_grade = Grade(100)
pieter_grade1 = Grade(80)
pieter_grade2 = Grade(60)
pieter.add_grade(pieter_grade)
pieter.add_grade(pieter_grade1)
pieter.add_grade(pieter_grade2)
pieter_grade.is_passing()
pieter_grade1.is_passing()
pieter_grade2.is_passing()
pieter.get_average()
pieter.availability()
print(pieter)
It has only two possible outcomes, either it is a true assertion, or it is false. That means we can return it without the if..else coming into play, at all.
As for the add_grade method, making it into a one-line return will take some convoluted logic that will not be superior to the simple if question, even if it does look clever.
self.grades += [grade] if type(grade) == Grade else []