[ Attributes Python Adding a Set value ]
for Essay class I need The attribute that stores the grade should be public and always be betweem 0.0 and 4.0. What I was thinking was set_grade(self,grade) but how do you implement an if statement to make sure that it return a number 0.0 between 4.0
class Assessment:
grade = 0
def get_grade(self):
return self.grade
"""This operation is creating a concrete subclass of Assessment"""
class Essay (Assessment):
"""This operation is getting the grade the grade for the Essay class which will return a grade"""
def get_grade(self):
return self.grade
"""This operation is creating a TeamProject Class with an individual score and a team score grade"""
class Teamproject(Assessment):
ind_sc = 0
ts_sc = 0
"""This operation is getting the grade to individual score and team score and returning the average"""
def get_grade(self):
return (self.ind_sc +self.ts_sc) / 2
Answer 1
how do you implement an if statement to make sure that it return a number 0.0 between 4.0
Try this:
def set_grade(self, grade):
if grade < 0.0 or grade > 4.0:
raise ValueError('out of range')
self.grade = grade
If you use this in Assessent and Essay, the TeamScore will also be in-bounds.
Hope this helps :-)
Answer 2
if 0.0 <= grade <= 4.0:
<do something>
else:
raise ValueError("Error message)