|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- <div>import time as t</div><div>class Time():
-
- #开始计时
- def start(self):
- self.start=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])</div><div> print(self.prompt)
- </div>
复制代码- ======================== RESTART: E:/python/程序/44_计时器.py =======================
- >>> q=Time()
- >>> q.start()
- 计时开始
- >>> q.stop()
- Traceback (most recent call last):
- File "<pyshell#26>", line 1, in <module>
- q.stop()
- File "E:/python/程序/44_计时器.py", line 13, in stop
- self._calc()
- File "E:/python/程序/44_计时器.py", line 21, in _calc
- self.lasted.append(self.stop[index]-self.start[index])
- TypeError: 'builtin_function_or_method' object is not subscriptable
复制代码
不知道为什么错了?并且想知道怎么改?
本帖最后由 sunrise085 于 2020-8-4 19:08 编辑
共有三处两种错误,帮你修改了,在注释中已经写出来了。
1.类变量名与类函数名重复了
2.localtime是函数,调用函数需要加括号
- import time as t
- class Time():
-
- #开始计时
- def start(self):
- self.begin=t.localtime()#1.这里类变量名与类函数名重复了;2.localtime是函数名,需要加括号才是调用函数
- print('计时开始')
-
- #停止计时
- def stop(self):
- self.end=t.localtime()#1.这里类变量名与类函数名重复了;2.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])#stop和start同样需要改为end和begin
- self.prompt +=str(self.lasted[index])
- print(self.prompt)
复制代码
|
|