def __add__(self, other):
propmt = '总共运行了'
result = []
for index in range(6):
result.append(self.lasted[index] + other.lasted[index])
if result[index]:
prompt += (str(result[index]) + self.unit[index])
return prompt
你忘记加self 了
import time as t
class MyTimer():
def __init__(self):
self.unit = ['年', '月', '日', '时', '分', '秒']
self.prompt = "未开始计时"
self.lasted = []
self.begin = 0
self.end = 0
def __repr__(self):
return self.prompt
def __add__(self, other):
propmt = '总共运行了'
result = []
for index in range(6):
result.append(self.lasted[index] + other.lasted[index])
if result[index]:
self.prompt += (str(result[index]) + self.unit[index])
return self.prompt
def start(self):
self.begin = t.localtime()
self.prompt = "请先调用stop停止计时"
print("计时开始:")
# 停止计时
def stop(self):
if not self.begin:
print('请先计时')
else:
self.end = t.localtime()
self._calc()
print("计时结束")
# 计算计时时间
def _calc(self):
self.listed = []
self.prompt = '总共运行了'
for index in range(6):
self.listed.append((self.end[index] - self.begin[index]))
if self.listed[index] < 0:
# 秒:
if self.unit[index] in self.unit[5]: # 索引值每个确定,当相减的后者为负值的时候,向前借一位
self.listed[index - 1] -= 1 # 按我的想法是,即使前面那位是0,被借了变成-1,也可以在下次循环再往前补回来,但是结果却不行,问问为啥啊...
self.listed[index] = 60 + self.listed[index]
# 分:
elif self.unit[index] == self.unit[4]:
self.listed[index - 1] -= 1
self.listed[index] = 60 + self.listed[index]
# 时:
elif self.unit[index] == self.unit[3]:
self.listed[index - 1] -= 1
self.listed[index] = 24 + self.listed[index]
# 日:
elif self.unit[index] == self.unit[2]:
self.listed[index - 1] -= 1
self.listed[index] = 31 + self.listed[index]
# 月:
elif self.unit[index] == self.unit[1]:
self.listed[index - 1] -= 1
self.listed[index] = 12 + self.listed[index]
for index in range(6):
if self.lasted[index] > 0:
self.prompt += str(self.lasted[index]) + self.unit[index]
|