|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import time as t
class m:
def __str__(self):
return self.prompt
__repr__ = __str__
#定义时间相加
def __add__(self,other):
tips = '相加之后,总共运行了'
result = []
print(other.lasted)
print(self.lasted)
for index in range(6):
result.append(self.lasted[index] + other.lasted[index])
print(index)
if result[index]:
tips += (str(result[index]) + self.un[index])
return tips
#定义参数
def __init__(self):
self.begin = 0
self.end = 0
self.lasted = []
self.prompt = '还没有开始计时啦,先用start好么?'
self.un = ['年','月','天','小时','分','秒']
#开始计时
def start(self):
self.begin = t.localtime()
print('开始计时')
#结束计时
def stop(self):
self.end = t.localtime()
if self.begin == 0:
print('请先调用start()方法!!')
else:
self.calc()
print('结束计时')
#计算计时时间
def calc(self):
self.prompt = '总共运行了'
lasted = []
for index in range(6):
lasted.append(self.end[index] - self.begin[index])
if lasted[index]:
self.prompt += (str(lasted[index]) + self.un[index])
print (list(lasted))
self.begin = 0
self.start = 0
>>> a = m()
>>> a.start()
开始计时
>>> a.stop()
结束计时
>>> b = m()
>>> b.start()
开始计时
>>> b.stop()
结束计时
>>> a + b
[]
[]
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
a + b
File "D:/Python34/test13.py", line 16, in __add__
result.append(self.lasted[index] + other.lasted[index])
IndexError: list index out of range |
|