|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:import time as t
class MyTimer():
def __init__(self):
self.unit=['年','月','日','时','秒']
self.prompt='未开始计时'
self.begin=0
self.end=0
self.lasted=[]
def __str__(self):
return self.prompt
__repr__=__str__
#计时开始
def start(self):
self.begin = t.localtime()
self.prompt='提示:请先调用stop函数,停止计时'
print('计时开始:')
#计时结束
def stop(self):
if not self.begin:
print('提示:请先调用begin函数,开始计时')
else:
self.end= t.localtime()
self._calc()
print('计时结束:')
#时间计算
def _calc(self):
self.lasted=[]
self.prompt='总共运行了:'
for index in range(6):
self.lasted.append(self.end[index]-self.begin[index])
if lasted[index]:
self.prompt +=str(self.lasted[index]+self.unit[index])
#print(self.prompt)
self.begin=0
self.end=0
错误提示:
>>> f=MyTimer()
>>> f.start()
计时开始:
>>> f
提示:请先调用stop函数,停止计时
>>> f.stop()
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
f.stop()
File "C:\Users\xiaoqian\Desktop\时间重置计时.py", line 24, in stop
self._calc()
File "C:\Users\xiaoqian\Desktop\时间重置计时.py", line 32, in _calc
if lasted[index]:
NameError: name 'lasted' is not defined
>>> |
|