tiger吴 发表于 2020-6-29 17:52:13

44讲第0题简单定制

import time

#def一个函数当计算出的时间列表里面有负数时自动调整为需要的没有负数的数据
def adjust(mylist,index):
    date=
    if mylist:
      mylist-=1
      mylist+=date
    else:
      index-=1
      adjust(mylist,index)

class Timer:
    def start(self):
      self.begin=time.localtime
      print('计时开始')
    def stop(self):
      self.end=time.localtime
      self.__calc()
      
      
    def __calc(self):
      unit=['年','月','日','时','分','秒']
      
      self.lasted=''
      self.mylist=[]
      
      for index in range(6):
            self.mylist.append(self.end-self.begin)
      for index in range(6):         
            if self.mylist<0:
                index-=1
                adjust(mylist,index)
      for index in range(6):
            if self.mylist:
                self.lasted+=str(self.mylist)+unit
      print('计时结束'+'\n'+'共运行%s'%self.lasted)

t=Timer()
t.start()
t.stop()
self.mylist.append(self.end-self.begin)系统报告这行类型错误,函数不可加下标,
大佬们帮我改下程序,让他能跑起来

qiuyouzhi 发表于 2020-6-29 18:02:16

你的self.begin和self.end是俩函数啊

Twilight6 发表于 2020-6-29 18:22:25




import time


# def一个函数当计算出的时间列表里面有负数时自动调整为需要的没有负数的数据
def adjust(mylist, index):
    date =
    if mylist:
      mylist -= 1
      mylist += date
    else:
      index -= 1
      adjust(mylist, index)


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

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

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

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

      for index in range(6):
            self.mylist.append(self.end - self.begin)
      for index in range(6):
            if self.mylist < 0:
                index -= 1
                adjust(self.mylist, index)
      for index in range(6):
            if self.mylist:
                self.lasted += str(self.mylist) + unit
      print('计时结束' + '\n' + '共运行%s' % self.lasted)


t = Timer()
t.start()

Twilight6 发表于 2020-6-29 18:25:19

self.begin=time.localtime
这边忘记加上括号了
self.end=time.localtime
同上
for index in range(6):         
            if self.mylist<0:
                index-=1
                adjust(mylist,index)

这个地方忘记加 self 了



我上面的代码没帮你停止不然你看不到运行时间哈你还是运行之后 然后输入 t.stop() 在停止吧

tiger吴 发表于 2020-6-29 19:29:43

Twilight6 发表于 2020-6-29 18:25
这边忘记加上括号了

同上


嗯,但是手机端不允许运行后再输入,所以我只能这样试试了,发出来主要是那个报错,我看着那一行应该没有错,前面加了两括号后正常跑起来了

Twilight6 发表于 2020-6-29 20:01:04

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



嗯嗯    我代码帮您改好了也测试过了你复制去就可以了~~

tiger吴 发表于 2020-6-29 22:39:46

Twilight6 发表于 2020-6-29 20:01
嗯嗯    我代码帮您改好了也测试过了你复制去就可以了~~

最后面加上t.stop()
应该也会有运行的时间数啊,为什么是空的呢
页: [1]
查看完整版本: 44讲第0题简单定制