|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import time as t
class MyTimer():
def __init__(self):
self.unit=['year','month','day','hours','minute','sec']
self.prompt='wei kaishi jishi '
self.lasted=[]
self.begin=0
self.end=0
def __str__(self):
return self.prompt
__repr__=__str__
def start(self):
self.begin = t.localtime()
self.prompt='pleasd stop!'
print("jishikaishi")
def stop(self):
if not self.begin:
print('please start')
else:
self.end= t.localtime()
self._calc()
print("jishijieshu")
def _calc(self):
self.lasted=[]
self.prompt='zonggongyunxingle'
for index in range(6):
self.lasted.append(self.end[index] - self.begin[index])
if self.lasted[i]:
self.prompt+=(str(self.lasted[index])+self.unit[index])
self.begin=0
self.end=0
运行结果:
>>> a=MyTimer()
>>> a.start()
jishikaishi
>>> a.stop()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
a.stop()
File "D:\PYthon\练习\计时.py", line 28, in stop
self._calc()
File "D:\PYthon\练习\计时.py", line 36, in _calc
if self.lasted[i]:
NameError: name 'i' is not defined
嗯 你第一个错误上面说了哈 就是,index 打成了 i ,第二个错误就是 self.begin=0 和 self.end=0
写在了 for 循环内,导致一次循环就把原先的 begin 和 end 的值赋值为了 int 整型,而下次用列表索引时候就会报错
把 self.begin=0 和 self.end=0 移动到 for 循环外即可
|
|