鱼C论坛

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

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

[复制链接]
发表于 2020-6-29 17:52:13 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
  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.         
  19.         
  20.     def __calc(self):
  21.         unit=['年','月','日','时','分','秒']
  22.         
  23.         self.lasted=''
  24.         self.mylist=[]
  25.         
  26.         for index in range(6):
  27.             self.mylist.append(self.end[index]-self.begin[index])
  28.         for index in range(6):         
  29.             if self.mylist[index]<0:
  30.                 index-=1
  31.                 adjust(mylist,index)
  32.         for index in range(6):
  33.             if self.mylist[index]:
  34.                 self.lasted+=str(self.mylist[index])+unit[index]
  35.         print('计时结束'+'\n'+'共运行%s'%self.lasted)

  36. t=Timer()
  37. t.start()
  38. t.stop()
复制代码

self.mylist.append(self.end[index]-self.begin[index])系统报告这行类型错误,函数不可加下标,
大佬们帮我改下程序,让他能跑起来
最佳答案
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:02:16 | 显示全部楼层
你的self.begin和self.end是俩函数啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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 19:29:43 From FishC Mobile | 显示全部楼层
Twilight6 发表于 2020-6-29 18:25
这边忘记加上括号了

同上

嗯,但是手机端不允许运行后再输入,所以我只能这样试试了,发出来主要是那个报错,我看着那一行应该没有错,前面加了两括号后正常跑起来了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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



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

使用道具 举报

 楼主| 发表于 2020-6-29 22:39:46 From FishC Mobile | 显示全部楼层
Twilight6 发表于 2020-6-29 20:01
嗯嗯    我代码帮您改好了  也测试过了  你复制去就可以了~~

最后面加上t.stop()
应该也会有运行的时间数啊,为什么是空的呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 20:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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