|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import time as t
class Timer():
def __init__(self):
self.units=['Year','Month','Day','hour','minute','second']
self.prompt='the timer is yet to start'
self.begin=0
self.end=0
self.lasted=[]
def __str__(self):
return self.prompt
__repr__=__str__
def ___add__(self,other):
result=[]
prompt='two of them totally took '
for index in range(6):
result.append(self.lasted[index]+other.lasted[index])
if result[index]:
prompt+=(str(result[index])+self.units[index])
return prompt
def start(self):
self.begin=t.localtime()
self.prompt='Please call the stop function to stop'
print ('Timer starts right now!')
def stop(self):
if not self.begin:
print ('Please call the start funcation to start the timer.')
else:
self.end=t.localtime()
self.__calc()
print ('The timer stops now.')
def __calc(self):
self.lasted=[]
self.prompt='It took '
for index in range(6):
self.lasted.append(self.end[index]-self.begin[index])
if self.lasted[index]:
self.prompt+=(str(self.lasted[index])+self.units[index])
self.begin=0
self.end=0
错误提示:
>>> t1+t2
Traceback (most recent call last):
File "<pyshell#202>", line 1, in <module>
t1+t2
TypeError: unsupported operand type(s) for +: 'Timer' and 'Timer'
我快哭了,想了一个小时也没想明白 |
|