鱼C论坛

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

[已解决]魔法方法:简单定制 课堂例题出错

[复制链接]
发表于 2020-6-6 07:36:35 | 显示全部楼层 |阅读模式

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

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

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.     def __str__(self):
  10.         return self.prompt

  11.     __repr__ = __str__

  12.     def __add__(self,other):
  13.         prompt = "总共运行了"
  14.         result = []
  15.         for index in range(6):
  16.             result.append(self.lasted[index] + other.lasted[index])
  17.             if result[index]:
  18.                 prompt += (str(result[index]) + self.unit[index])
  19.         return prompt

  20.     # 开始计时
  21.     def start(self):
  22.         self.begin = t.localtime()
  23.         self.prompt = "提示:请先调用 stop() 停止计时!"
  24.         print("计时开始...")

  25.     # 停止计时
  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.     # 内部方法,计算运行时间
  34.     def _calc(self):
  35.         self.lasted = []
  36.         self.prompt = "总共运行了"
  37.         for index in range(6):
  38.             self.lasted.append(self.end(index) - self.begin[index])
  39.             if self.lasted[index]:
  40.                 self.prompt += (str(self.lasted[index]) + self.unit[index])
  41.         # 为下一轮计时初始化变量
  42.         self.begin = 0
  43.         self.end = 0
复制代码
最佳答案
2020-6-6 08:04:14
本帖最后由 sunrise085 于 2020-6-6 08:06 编辑

照着视频打的,也打错了。
self.lasted.append(self.end(index) - self.begin[index])
你没发现这一行的两个index不一样么?index是下标,end后面的index你放在小括号里了,下标应该放进中括号里面。
错误提示很明显啊,小括号是调用,错误提示说,类型错误,time.struct_time类型的object不能被调用。
B{%0(JFJ$8BEY_HHIFXP77V.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-6 07:37:06 | 显示全部楼层
照着视频打的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-6 07:51:12 | 显示全部楼层
self.lasted.append(self.end(index) - self.begin[index])
这里的end是一个time.struct_time类型的object
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-6-6 07:58:35 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-6 08:04:14 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sunrise085 于 2020-6-6 08:06 编辑

照着视频打的,也打错了。
self.lasted.append(self.end(index) - self.begin[index])
你没发现这一行的两个index不一样么?index是下标,end后面的index你放在小括号里了,下标应该放进中括号里面。
错误提示很明显啊,小括号是调用,错误提示说,类型错误,time.struct_time类型的object不能被调用。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-6 08:11:50 | 显示全部楼层
  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.     def __str__(self):
  10.         return self.prompt

  11.     __repr__ = __str__

  12.     def __add__(self,other):
  13.         prompt = "总共运行了"
  14.         result = []
  15.         for index in range(6):
  16.             result.append(self.lasted[index] + other.lasted[index])
  17.             if result[index]:
  18.                 prompt += (str(result[index]) + self.unit[index])
  19.         return prompt

  20.     # 开始计时
  21.     def start(self):
  22.         self.begin = t.localtime()
  23.         self.prompt = "提示:请先调用 stop() 停止计时!"
  24.         print("计时开始...")

  25.     # 停止计时
  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.     # 内部方法,计算运行时间
  34.     def _calc(self):
  35.         self.lasted = []
  36.         self.prompt = "总共运行了"
  37.         for index in range(6):
  38.             self.lasted.append(self.end[index] - self.begin[index])
  39.             if self.lasted[index]:
  40.                 self.prompt += (str(self.lasted[index]) + self.unit[index])
  41.         # 为下一轮计时初始化变量
  42.         self.begin = 0
  43.         self.end = 0
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-6 08:26:04 | 显示全部楼层
sunrise085 发表于 2020-6-6 08:04
照着视频打的,也打错了。
self.lasted.append(self.end(index) - self.begin)
你没发现这一行的两个inde ...

打着打着打糊涂了啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 10:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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