鱼C论坛

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

[已解决]044讲 关于定义计时器的时间计算错误

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

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

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

x
  1. import time as t

  2. class Mytime():
  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.     __repr__ = __str__

  13.     #开始计时
  14.     def start(self):
  15.         self.begin = t.localtime()
  16.         self.prompt = '提示:请先调用stop()停止计时!'
  17.         print('计时开始。。。')

  18.     # 停止计时
  19.     def stop(self):
  20.         if not self.begin:
  21.             print('提示:请先调用start()进行计时!')
  22.         else:
  23.             self.end = t.localtime()
  24.             self._calc()
  25.             print('计时结束!')


  26.     # 内部方法,计算运行时间
  27.     def _calc(self):
  28.         self.lasted = []
  29.         self.prompt = '总共运行了'
  30.         for index in range(6):
  31.             self.lasted.append(self.end[index] - self.begin[index])
  32.             if self.lasted[index]:
  33.                 self.prompt += (str(self.lasted[index]) + self.unit[index])

  34.         # 为下一步计时初始化变量
  35.         self.begin = 0
  36.         self.end = 0

  37. t1 = Mytime()   
复制代码


问题:
我的时间计算结果是错误的,好像是实际计时结果的很多倍,这是那个地方的问题呢?
最佳答案
2016-10-7 11:53:16
self.unit = ['年','月','天','小时','小时','分钟','秒']
时间计算.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-10-7 11:09:26 | 显示全部楼层
  1. import time as t

  2. class Mytime():
  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.     __repr__ = __str__

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

  21.     #开始计时
  22.     def start(self):
  23.         self.begin = t.localtime()
  24.         self.prompt = '提示:请先调用stop()停止计时!' # 调用start,将‘提示:请先调用stop()停止计时!’写入prompt,如直接调用t1,则return self.prompt
  25.         print('计时开始。。。')

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


  34.     # 内部方法,计算运行时间
  35.     def _calc(self):
  36.         self.lasted = []
  37.         self.prompt = '总共运行了'
  38.         for index in range(6):
  39.             self.lasted.append(self.end[index] - self.begin[index])
  40.             if self.lasted[index]:
  41.                 self.prompt += (str(self.lasted[index]) + self.unit[index])

  42.         # 为下一步计时初始化变量
  43.         self.begin = 0
  44.         self.end = 0

  45. t1 = Mytime()   
复制代码


时间计算也错误.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-10-7 11:13:02 | 显示全部楼层
还有,这个代码里面的【index】代表什么意思?

路过鱼油帮帮忙,谢谢啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-7 11:40:36 From FishC Mobile | 显示全部楼层
定义的计时单位有2个‘小时’单位,这个应该是写错了吧? 另外,index就是一个(0~5)的元组,表示依次循环提取时间的各单位

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
人zai旅途 + 1 + 1 十分感谢帮忙指点

查看全部评分

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

使用道具 举报

发表于 2016-10-7 11:53:16 | 显示全部楼层    本楼为最佳答案   
self.unit = ['年','月','天','小时','小时','分钟','秒']

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
人zai旅途 + 1 + 1 感谢大神回帖解惑

查看全部评分

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

使用道具 举报

 楼主| 发表于 2016-10-7 12:37:36 | 显示全部楼层
SixPy 发表于 2016-10-7 11:53
self.unit = ['年','月','天','小时','小时','分钟','秒']


谢谢大神
我犯了低级错误,惭愧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-10-7 12:42:40 | 显示全部楼层
jerryxjr1220 发表于 2016-10-7 11:40
定义的计时单位有2个‘小时’单位,这个应该是写错了吧? 另外,index就是一个(0~5)的元组,表示依次循 ...

谢谢了,是我打错代码了,,惭愧啊;
【index】我也明白了,文档里面都有,是我没看清楚。
谢谢帮忙,太感谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-23 05:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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