2013-04-18 73 views
1

對於論文類我需要存儲等級的屬性應該是公開的,並且始終在0.0到4.0之間。我在想什麼是set_grade(個體經營,等級),但你怎麼實現if語句,以確保其返回4.0屬性Python添加設置值

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

doc字符串像? @LennartRegebro – user2267830

+0

http://www.python.org/dev/peps/pep-0257/ http://www.pythonforbeginners.com/basics/python-docstrings/ –

回答

0

之間的數字0.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 

如果您在Assessent使用和散文,TeamScore也將進入界限。

希望這有助於:-)

+0

'x'是'set_grade'中的一個未初始化的變量 – mtadd

0
if 0.0 <= grade <= 4.0: 
    <do something> 
else: 
    raise ValueError("Error message)