鱼C论坛

 找回密码
 立即注册
查看: 1640|回复: 1

计时器优化问题

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

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

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

x
本帖最后由 xizi123 于 2020-11-20 00:01 编辑

求大神指教:
问题1,为什么在stop()函数中,不能用self.prompt来做提示,而必须用print来打印提示内容(标红的句子)
问题2,在计时器中含有负数时,优化可否用我的方法,是否可行?
谢谢!!!
import time
class MyTime():
       def __init__(self):
              self.begin =0
              self.end = 0
              self.lasted = []
              self.unit = ['年','月','天','小时','分钟','秒']
              self.prompt = '提示:程序未运行'
              self.zh = [1,12,31,24,60,60]
       def start(self):
              self.begin = time.localtime()
              self.prompt ='提示:请先运行stop()程序'
              print('计时开始......')

       def stop(self):
              if self.begin:
                     self.end = time.localtime()
                     self.prompt = '计时结束'
                     self._calc()
              else:
                     print('提示:请先运行start()程序')
       #内部程序,计算时间
       def _calc(self):
              
              self.prompt = '程序共运行了'

       #转换带有负数的时间
       #由地位向高位依次按照 负数时间加上进率时间 在向上一位借1的思路进行计算
              dd= []
              temp = []
              count = 0
              for index in range(6):
                     temp.append(self.end[index]-self.begin[index])
              for index in range(6):
                     if temp[5-index] < 0:
                            dd.append(temp[5-index] + self.zh[5-index] +count)
                           
                            count = -1

                     else:
                            dd.append(temp[5-index]+count)
                            count = 0
              dd.reverse()
              print('dd = ',dd)
              self.lasted = dd[:]




              
              
              for index in range(6):
                     

                     if self.lasted[index]:
                            self.prompt += str(self.lasted[index]) + self.unit[index]

              print(self.prompt)
              self.begin = 0
              self.end = 0

       def __str__(self):
              return self.prompt

       __repr__ = __str__

       def __add__ (self,other):
              result = []
              prompt = '程序共运行了'
              for index in range(6):
                     result.append(self.lasted[index]+other.lasted[index])
                     if result[index]:
                            prompt += str(result[index]) + self.unit[index]
              print(prompt)


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-22 12:33:42 | 显示全部楼层
本帖最后由 suchocolate 于 2020-11-22 13:58 编辑

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-22 02:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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