2016-12-03 39 views
0

我想了解爲什麼我的方法increment4無法正常工作。所有這些方法都可以很好地作爲函數,但現在我已經將它們轉換成了一些不起作用的方法。我用「自己」替換了「時間」對象名稱。我嘗試保留並刪除「返回」。 編輯我沒有使用點符號,正如我所指出的那樣。我做了一些改變。現在,Python是給我一個不同的錯誤:33Python,向類中添加方法,錯誤:全局名稱未定義

Traceback (most recent call last): 
    File "/Users//Desktop/temp.py", line 33, in <module> 
    currenttime.increment4(4000) 
    File "/Users//Desktop/temp.py", line 22, in increment4 
    newtime = float(total_seconds).make_time() 
AttributeError: 'float' object has no attribute 'make_time' 
>>> 

線路是:

currenttime.increment4(4000) 

線22:

newtime = float(total_seconds).make_time() 

這裏是整個事情:

class Time: 
    def printTime(self): 
     print str(time.hours)+":"+str(time.minutes)+":"+str(time.seconds) 

    def make_time (self): 
     time = Time() 
     time.hours = self/3600 
     time.minutes = (self % 3600)/60 
     time.seconds = (self % 3600) % 60 
     return time 

    def covertTOseconds (self): 
     hours = self.hours * 3600 
     minutes = self.minutes * 60 
     seconds = self.seconds 
     amt_in_seconds = hours + minutes + seconds 
     return amt_in_seconds 

    def increment4 (self, increaseINseconds): 
     total_seconds = self.covertTOseconds() + increaseINseconds 
     newtime = float(total_seconds).make_time() 
     newtime.printTime() 


currenttime = Time() 
currenttime.hours = 3 
currenttime.minutes = 47 
currenttime.seconds = 45 



currenttime.increment4(4000) 
+1

'self.covertTOseconds()',和每個PEP-8('convert_to_seconds')固定命名約定。 – jonrsharpe

+0

謝謝喬恩!我實施了你的建議,我正在編輯我的問題。現在拋出一個不同的錯誤。 –

+0

而錯誤信息告訴你問題是什麼。 – jonrsharpe

回答

0

您需要稍微修改一下代碼:

class Time: 
    def printTime(self, time): 
     print str(time.hours)+":"+str(time.minutes)+":"+str(time.seconds) 

    def increment4(self, increaseINseconds): 
     seconds = self.covertTOseconds() + increaseINseconds 
     time = self.makeTime (seconds) 
     print time 

    def makeTime(self, totalseconds): 
     time = Time() 
     time.hours = totalseconds/3600 
     time.minutes = (totalseconds % 3600)/60 
     time.seconds = (totalseconds % 3600) % 60 
     return time 

    def covertTOseconds(self): 
     hours = self.hours * 3600 
     minutes = self.minutes * 60 
     seconds = self.seconds 
     totalseconds = hours + minutes + seconds 
     return totalseconds 

由於錯誤顯示converTOseconds沒有定義,因爲它應該self被調用,也它不帶任何參數。因此,您需要將covertTOseconds (self)更改爲self.convertTOseconds()。希望這可以幫助。

既然你已經更新的問題,在我看來,那是你可能想要做的是有這樣一個類:

class Time: 
    def __init__(self): 
     self.hours = None 
     self.minutes = None 
     self.seconds = None 
     self.total_seconds = None 

    def to_seconds(self): 
     hours = self.hours * 3600 
     return (self.hours * 3600) + (self.minutes * 60) + self.seconds 

    def increment(self, x): 
     self.total_seconds = self.to_seconds() + x 
     self.hours = self.total_seconds/3600 
     self.minutes = (self.total_seconds % 3600)/60 
     self.seconds = (self.total_seconds % 3600) % 60 
     self.__str__() 

    def __str__(self): 
     print("{0}:{1}:{2}".format(self.hours, self.minutes, self.seconds)) 

然後你可以使用這個類,如下所示:

c_time = Time() 
c_time.hours = 3 
c_time.minutes = 47 
c_time.seconds = 45 

c_time.increment(4000) 

此輸出:

4:54:25 
+0

謝謝何塞。我根據您的建議進行了一些編輯。現在我得到了一個不同的錯誤:AttributeError:'int'對象沒有屬性'makeTime'。我無法弄清楚這裏缺少的概念是什麼...... –

+0

您已經修改了您的方法'increment4',它嘗試在整數類型'total_seconds'上調用方法'makeTime'。 –

+0

對不起,我看到你的編輯之前寫了這個!在我問另一個問題之前,我會嘗試它們。謝謝! –

相關問題