鱼C论坛

 找回密码
 立即注册
查看: 3727|回复: 6

[已解决]请问一下这个应该怎么改?老是出现TypeError: 'int' object is not subscriptable

[复制链接]
发表于 2018-11-18 20:44:07 | 显示全部楼层 |阅读模式

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

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

x
import time as t

class MyTimer:

    def __init__(self):
        self.begin = 0
        self.end = 0
        self.prompt = "before the time is running"
        self.lasted = []
        self.unit = ["year","month","day","hour","minute","second"]
        self.word = ""

    def __str__(self):
        return self.prompt

    __repr__ = __str__

    def start(self):
        self.begin = t.localtime()
        print("the time is running!")


    def stop(self):
        if not self.begin:
            print("You don't have run the method start")
            print("Do you want me stat the method? if yes I will start if or I  will quit ")
            self.word = input("plz input yes or no:")
            if self.word == "yes":
                self.end = t.localtime()
                print("the timer is end")
                self._clac()
            else:
                print("The Timer is over")
                raise KeyboardInterrupt
               
        else:
            self.end = t.localtime()
            print("the timer is end")
            self._clac()
   # an inner method

    def _clac(self):

        self.prompt = "all of time is "
        for index in range(6):
            self.lasted.append(self.end[index] - self.begin[index])
            if self.lasted[index] != 0:
                self.prompt += str(self.lasted[index]) + " " + self.unit[index]
      
最佳答案
2018-11-18 21:11:48
你设计的功能,自己start,由于你的begin还是0,不能用[]索引
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-18 20:50:31 | 显示全部楼层
你把出错截图发出来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-18 21:05:13 | 显示全部楼层
塔利班 发表于 2018-11-18 20:50
你把出错截图发出来

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    f.stop()
  File "F:\python\Timer.py", line 31, in stop
    self._clac()
  File "F:\python\Timer.py", line 49, in _clac
    self.lasted.append(self.end[index] - self.begin[index])
TypeError: 'int' object is not subscriptable
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-18 21:11:48 | 显示全部楼层    本楼为最佳答案   
你设计的功能,自己start,由于你的begin还是0,不能用[]索引
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-18 21:13:11 | 显示全部楼层
GumpYu 发表于 2018-11-18 21:05
Traceback (most recent call last):
  File "", line 1, in
    f.stop()

代码,调式过程,截图,一个不能少。你这么搞还要别人帮你摸索?

人家说少什么你又上什么;自己不麻烦?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-18 21:25:57 | 显示全部楼层
ba21 发表于 2018-11-18 21:13
代码,调式过程,截图,一个不能少。你这么搞还要别人帮你摸索?

人家说少什么你又上什么;自己不麻烦 ...

第一次发帖,不好意思 ,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-18 21:27:19 | 显示全部楼层
塔利班 发表于 2018-11-18 21:11
你设计的功能,自己start,由于你的begin还是0,不能用[]索引

哦哦哦  3Q
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-28 19:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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