错误显示变量没有被定义,但我觉得已经定义了,求大佬看看应该如何修改
代码:import time as tclass 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-self.begin)
if lasted:
self.prompt +=str(self.lasted+self.unit)
#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:
NameError: name 'lasted' is not defined
>>> self.lasted.append(self.end-self.begin)
self.begin=0
self.end=0
self.end不是一个整数变量吗?还能取索引? 第32行少个self
if self.lasted
页:
[1]