我正在exorcism.io時鐘練習,我無法弄清楚爲什麼這個測試失敗。結果看起來完全相同,甚至具有相同的類型。在Python 3.52中的字符串或對象compairson
這裏是我的代碼:
class Clock:
def __init__(self, h, m):
self.h = h
self.m = m
self.adl = 0
def make_time(self):
s = self.h * 3600
s += self.m * 60
if self.adl: s += self.adl
while s > 86400:
s -= 86400
if s == 0:
return '00:00'
h = s // 3600
if h:
s -= h * 3600
m = s // 60
return '{:02d}:{:02d}'.format(h, m)
def add(self, more):
self.adl = more * 60
return self.make_time()
def __str__(self):
return str(self.make_time()) # i don't think I need to do this
if __name__ == '__main__':
cl1 = Clock(34, 37) #10:37
cl2 = Clock(10, 37) #10:37
print(type(cl2))
print(cl2, cl1)
print(cl2 == cl1) #false
您還沒有爲這些對象定義一個相等比較,所以它們從'object'繼承了默認的基於標識的'=='。 – user2357112
@ user2357112就是這樣。謝謝!還需要將新的小時和分鐘放在自己的字典中。 – Eman
請不要爲您的問題添加解決方案。歡迎您在下面添加您自己的答案。請記住,堆棧溢出帖子意味着對未來的訪問者有同樣的問題,並且答案是獨立投票的。 –