鱼C论坛

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

[已解决]44讲第0题简单定制

[复制链接]
发表于 2020-6-29 18:22:25 | 显示全部楼层    本楼为最佳答案   



  1. import time


  2. # def一个函数当计算出的时间列表里面有负数时自动调整为需要的没有负数的数据
  3. def adjust(mylist, index):
  4.     date = [0, 12, 30, 24, 60, 60]
  5.     if mylist[index]:
  6.         mylist[index] -= 1
  7.         mylist[index + 1] += date[index]
  8.     else:
  9.         index -= 1
  10.         adjust(mylist, index)


  11. class Timer:
  12.     def start(self):
  13.         self.begin = time.localtime()
  14.         print('计时开始')

  15.     def stop(self):
  16.         self.end = time.localtime()
  17.         self.__calc()

  18.     def __calc(self):
  19.         unit = ['年', '月', '日', '时', '分', '秒']

  20.         self.lasted = ''
  21.         self.mylist = []

  22.         for index in range(6):
  23.             self.mylist.append(self.end[index] - self.begin[index])
  24.         for index in range(6):
  25.             if self.mylist[index] < 0:
  26.                 index -= 1
  27.                 adjust(self.mylist, index)
  28.         for index in range(6):
  29.             if self.mylist[index]:
  30.                 self.lasted += str(self.mylist[index]) + unit[index]
  31.         print('计时结束' + '\n' + '共运行%s' % self.lasted)


  32. t = Timer()
  33. t.start()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-29 18:25:19 | 显示全部楼层
self.begin=time.localtime

这边忘记加上括号了
self.end=time.localtime

同上
for index in range(6):         
            if self.mylist[index]<0:
                index-=1
                adjust(mylist,index)


这个地方忘记加 self 了



我上面的代码没帮你停止  不然你看不到运行时间哈  你还是运行之后 然后输入 t.stop() 在停止吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-29 20:01:04 | 显示全部楼层
tiger吴 发表于 2020-6-29 19:29
嗯,但是手机端不允许运行后再输入,所以我只能这样试试了,发出来主要是那个报错,我看着那一行应该没有 ...



嗯嗯    我代码帮您改好了  也测试过了  你复制去就可以了~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-20 03:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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