我想了解爲什麼我的方法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)
'self.covertTOseconds()',和每個PEP-8('convert_to_seconds')固定命名約定。 – jonrsharpe
謝謝喬恩!我實施了你的建議,我正在編輯我的問題。現在拋出一個不同的錯誤。 –
而錯誤信息告訴你問題是什麼。 – jonrsharpe