鱼C论坛

 找回密码
 立即注册
查看: 1311|回复: 5

[已解决]44讲课上例题

[复制链接]
发表于 2021-9-8 10:44:58 | 显示全部楼层 |阅读模式

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

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

x
  1. import time as t

  2. class MyTimer():
  3.     def __init__(self):
  4.         self.unit = ['年','月','天','小时','分钟','秒']
  5.         self.prompt = "未开始计时!"
  6.         self.lasted = []
  7.         self.begin = 0
  8.         self.end = 0
  9.         
  10.     def __str__(self):
  11.         return self.prompt
  12.    
  13.     __repr__ = __str__

  14.     def __add__(self,other):
  15.         prompt = '总共运行了'
  16.         result = []
  17.         for index in range(6):
  18.             result.append(self.lasted[index] + other.lasted[index])
  19.             if result[index]:
  20.                 prompt += (str(result[index]) + self.unit[index])
  21.         return prompt
  22.     def start(self):
  23.         self.begin = t.localtime()
  24.         self.prompt = '提示:请先调用stop()停止计时!'
  25.         print('计时开始...')

  26.     def stop(self):
  27.         if not self.begin:
  28.             print('提示:请先调用start()开始计时!')
  29.         else:
  30.             self.end = t.localtime()
  31.             self._calc()
  32.             print('计时结束!')

  33.     def _calc(self):
  34.         self.lasted = []
  35.         self.prompt = '总共运行了'
  36.         for index in range(6):
  37.             self.lasted.append(self.end[index] - self.begin[index])
  38.             if self.lasted[index]:
  39.                 self.prompt += (str(self.lasted[index]) + self.unit[index])
  40.             self.begin = 0
  41.             self.end = 0


复制代码


出错:
  1. >>> t1 = MyTimer()
  2. >>> t1
  3. 未开始计时!
  4. >>> t1.stop()
  5. 提示:请先调用start()开始计时!
  6. >>> t1.start()
  7. 计时开始...
  8. >>> t1.stop()
  9. Traceback (most recent call last):
  10.   File "<pyshell#4>", line 1, in <module>
  11.     t1.stop()
  12.   File "D:\小甲鱼学习\时间.py", line 34, in stop
  13.     self._calc()
  14.   File "D:\小甲鱼学习\时间.py", line 41, in _calc
  15.     self.lasted.append(self.end[index] - self.begin[index])
  16. TypeError: 'int' object is not subscriptable
复制代码


帮帮忙
最佳答案
2021-9-8 11:12:15
西瓜味的苹果 发表于 2021-9-8 11:10
self.end - self.begin,这一段错了,你的end和begin都定义的是字符串,怎么能索引呢

看错了,不是字符串,你定义的是整型,整型是不能索引的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-8 10:47:31 | 显示全部楼层
怎么发悬赏
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-8 11:03:39 | 显示全部楼层
你的 self.end 和 self.begin 都不是列表啊(你在 class MyTimer() 類裡面已經定義是整數啊)第 41 行 self.lasted.append(self.end[index] - self.begin[index]),請問 self.end[index] 和 self.begin[index] 怎麼索引訪問?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-9-8 11:07:54 | 显示全部楼层

没事,不用悬赏
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-8 11:10:31 | 显示全部楼层

self.end[index] - self.begin[index],这一段错了,你的end和begin都定义的是字符串,怎么能索引呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-8 11:12:15 | 显示全部楼层    本楼为最佳答案   
西瓜味的苹果 发表于 2021-9-8 11:10
self.end - self.begin,这一段错了,你的end和begin都定义的是字符串,怎么能索引呢

看错了,不是字符串,你定义的是整型,整型是不能索引的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 23:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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