鱼C论坛

 找回密码
 立即注册
查看: 783|回复: 7

[已解决]定制一个计时器的类

[复制链接]
发表于 2020-4-6 17:01:05 | 显示全部楼层 |阅读模式

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

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

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.    
  15.     #开始计时
  16.     def start(self):
  17.         self.begin = t.localtime() #调用一个localime的方法
  18.         self.prompt = '提示:请先调用stop()结束计时'
  19.         print('计时开始')

  20.     #停止计时
  21.     def stop(self):
  22.         if not self.begin:
  23.             print("请先调用start()开始计时")
  24.         else:
  25.             self.end = t.localtime()
  26.             self._calc()         #调用一下计算内部时间的方法
  27.             print('计时结束')

  28.     #内部方法,计算运行时间
  29.     def _calc(self):
  30.         self.lasted = []
  31.         self.prompt = '总共运行了'
  32.         for index in range(6):
  33.             self.lasted.append(self.end[index] - self.begin[index])
  34.             if self.lasted[index]:
  35.                 self.prompt += (str(self.lasted[index]) + self.unit[index])
  36.         #为下一轮初始化变量
  37.         self.begin = 0
  38.         self.end = 0
  39.         #print(self.prompt)
复制代码



输出的是:
  1. >>> t1 = MyTimer()
  2. >>> t1.start()
  3. 计时开始
  4. >>> t1.stop()
  5. Traceback (most recent call last):
  6.   File "<pyshell#2>", line 1, in <module>
  7.     t1.stop()
  8.   File "G:/ZLuX/python/Timer.py", line 29, in stop
  9.     self._calc()         #调用一下计算内部时间的方法
  10.   File "G:/ZLuX/python/Timer.py", line 39, in _calc
  11.     self.prompt += (str(self.lasted[index]) + self.unit[index])
  12. TypeError: 'set' object is not subscriptable
  13. >>>
复制代码
最佳答案
2020-4-6 17:03:47
本帖最后由 qiuyouzhi 于 2020-4-6 17:20 编辑

那个self.unit因为没有映射关系,所以Python认为它是一个集合
集合又是无序的,你不能通过下标来访问
所以,你可以改成列表:
  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.    
  15.     #开始计时
  16.     def start(self):
  17.         self.begin = t.localtime() #调用一个localime的方法
  18.         self.prompt = '提示:请先调用stop()结束计时'
  19.         print('计时开始')

  20.     #停止计时
  21.     def stop(self):
  22.         if not self.begin:
  23.             print("请先调用start()开始计时")
  24.         else:
  25.             self.end = t.localtime()
  26.             self._calc()         #调用一下计算内部时间的方法
  27.             print('计时结束')

  28.     #内部方法,计算运行时间
  29.     def _calc(self):
  30.         self.lasted = []
  31.         self.prompt = '总共运行了'
  32.         for index in range(6):
  33.             self.lasted.append(self.end[index] - self.begin[index])
  34.             if self.lasted[index]:
  35.                 self.prompt += (str(self.lasted[index]) + self.unit[index])
  36.         #为下一轮初始化变量
  37.         self.begin = 0
  38.         self.end = 0
  39.         #print(self.prompt)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-6 17:02:15 | 显示全部楼层
请问一下是哪里出错了呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 17:02:19 | 显示全部楼层
试试这样:

  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.     # 开始计时
  13.     def start(self):
  14.         self.begin = t.localtime()  # 调用一个localime的方法
  15.         self.prompt = '提示:请先调用stop()结束计时'
  16.         print('计时开始')

  17.     # 停止计时
  18.     def stop(self):
  19.         if not self.begin:
  20.             print("请先调用start()开始计时")
  21.         else:
  22.             self.end = t.localtime()
  23.             self._calc()  # 调用一下计算内部时间的方法
  24.             print('计时结束')

  25.     # 内部方法,计算运行时间
  26.     def _calc(self):
  27.         self.lasted = []
  28.         self.prompt = '总共运行了'
  29.         for index in range(6):
  30.             self.lasted.append(self.end[index] - self.begin[index])
  31.             if self.lasted[index]:
  32.                 self.prompt += (str(self.lasted[index]) + self.unit[index])
  33.         # 为下一轮初始化变量
  34.         self.begin = 0
  35.         self.end = 0
  36.         # print(self.prompt)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-4-6 17:03:47 | 显示全部楼层    本楼为最佳答案   
本帖最后由 qiuyouzhi 于 2020-4-6 17:20 编辑

那个self.unit因为没有映射关系,所以Python认为它是一个集合
集合又是无序的,你不能通过下标来访问
所以,你可以改成列表:
  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.    
  15.     #开始计时
  16.     def start(self):
  17.         self.begin = t.localtime() #调用一个localime的方法
  18.         self.prompt = '提示:请先调用stop()结束计时'
  19.         print('计时开始')

  20.     #停止计时
  21.     def stop(self):
  22.         if not self.begin:
  23.             print("请先调用start()开始计时")
  24.         else:
  25.             self.end = t.localtime()
  26.             self._calc()         #调用一下计算内部时间的方法
  27.             print('计时结束')

  28.     #内部方法,计算运行时间
  29.     def _calc(self):
  30.         self.lasted = []
  31.         self.prompt = '总共运行了'
  32.         for index in range(6):
  33.             self.lasted.append(self.end[index] - self.begin[index])
  34.             if self.lasted[index]:
  35.                 self.prompt += (str(self.lasted[index]) + self.unit[index])
  36.         #为下一轮初始化变量
  37.         self.begin = 0
  38.         self.end = 0
  39.         #print(self.prompt)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 17:05:28 | 显示全部楼层
  1. self.unit = {'年','月','日','小时','分钟','秒'}
复制代码

错在你开头定义的类__init__这里,是set类,没有索引的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-6 17:49:21 | 显示全部楼层
qiuyouzhi 发表于 2020-4-6 17:03
那个self.unit因为没有映射关系,所以Python认为它是一个集合
集合又是无序的,你不能通过下标来访问
所 ...

谢谢你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-6 17:50:07 | 显示全部楼层
十月故里 发表于 2020-4-6 17:05
错在你开头定义的类__init__这里,是set类,没有索引的

非常感谢你的回答,很有帮助
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-6 17:50:46 | 显示全部楼层

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 00:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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