|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import time as t
- class MyTimer():
- #开始计时
- def start(self):
- self.star = t.localtime()
- print('记时开始')
- #停止计时
- def stop(self):
- self.stop = t.localtime()
- self._calc()
- print('停止计时')
-
- #内部方法:计算运行时间
- def _calc(self):
- self.lasted = []
- self.prompt ='总共运行了'
- for index in range(6):
- self.lasted.append(self.stop[index] - self.start[index])
- self.prompt += str(self.lasted[index])
- print(self.prompt)
复制代码
运行后出现错误
>>> t1=MyTimer()
>>> t1.start()
记时开始
>>> t1.stop()
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
t1.stop()
File "F:\小甲鱼练习\44-计时器.py", line 13, in stop
self._calc()
File "F:\小甲鱼练习\44-计时器.py", line 21, in _calc
self.lasted.append(self.stop[index] - self.start[index])
TypeError: 'method' object is not subscriptable
>>>
hi 原因如下
第6行代码笔误,self.star = t.localtime() ,应该为 :self.start = t.localtime() ,修改后调试通过
>>> a = MyTimer()
>>> a.start()
记时开始
>>> a.stop()
总共运行了000003
停止计时
|
|