对于Essay课我需要存储成绩的属性应该是公共的,并且总是在0.0和4.0之间。我在想的是set_grade(self,grade)但是如何实现if语句以确保它返回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
答案 0 :(得分:0)
如何实现if语句以确保它返回4.0之间的数字
试试这个:
def set_grade(self, grade):
if grade < 0.0 or grade > 4.0:
raise ValueError('out of range')
self.grade = grade
如果您在 Assessent 和 Essay 中使用此功能, TeamScore 也将在境内。
希望这会有所帮助: - )
答案 1 :(得分:0)
if 0.0 <= grade <= 4.0:
<do something>
else:
raise ValueError("Error message)