鱼C论坛

 找回密码
 立即注册
查看: 2153|回复: 3

学习044讲疑问,计时器执行报错:'method' object is not subscriptable

[复制链接]
发表于 2016-11-19 10:16:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)
执行结果:
>>> ti = Mytimer()
>>> ti.start()
计时开始
>>> ti.stop()
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    ti.stop()
  File "C:/Python34/42-1.py", line 9, in stop
    self._calc()
  File "C:/Python34/42-1.py", line 16, in _calc
    self.lasted.append(self.stop[index] - self.start[index])
TypeError: 'method' object is not subscriptable
>>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-11-19 10:25:50 | 显示全部楼层
    def start(self):

    def stop(self):
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-19 10:32:39 | 显示全部楼层
import time as t

class Mytimer():
    def _init_(self):
        self.prompt = "未开始计时!"
        self.lasted = []
        self.begin = 0
        self.end = 0
        
    def start(self):
        self.begin = t.localtime()
        print("计时开始")

    def stop(self):
        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])
            self.prompt += str(self.lasted[index])

        print(self.prompt)
重新初始化变量类型就可以了???
不知道为什么???
运行结果:
>>> t1.start()
计时开始
>>> t1.stop()
总共运行了000006
计时结束
>>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-6 22:29:56 From FishC Mobile | 显示全部楼层
楼主你是第四行start写成了star....游戏好难
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 08:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表