1

对于作文课,我需要存储成绩的属性应该是公开的,并且始终在 0.0 和 4.0 之间。我在想的是 set_grade(self,grade) 但是你如何实现一个 if 语句来确保它返回一个介于 4.0 之间的数字 0.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
4

2 回答 2

0

如何实现 if 语句以确保它返回 4.0 之间的数字 0.0

尝试这个:

  def set_grade(self, grade):
      if grade < 0.0 or grade > 4.0:
          raise ValueError('out of range')
      self.grade = grade

如果您在AssessentEssay中使用它,TeamScore也将在界内。

希望这可以帮助 :-)

于 2013-04-18T01:35:14.207 回答
0
if 0.0 <= grade <= 4.0:
    <do something>
else:
    raise ValueError("Error message)
于 2013-04-18T01:37:07.137 回答