【问题标题】:Python count down timer for date, hour, minute and secondPython倒计时日期,小时,分钟和秒
【发布时间】:2021-09-17 21:02:12
【问题描述】:

在第二个示例的this 链接中,我发现了一个倒计时计时器,但它只能工作几分钟和几秒钟,我怎样才能让它工作几个小时和几天。这是代码

import time 

def countdown(t): 

    while t: 
        mins, secs = divmod(t, 60) 
        timer = '{:02d}:{:02d}'.format(mins, secs) 
        print(timer, end="\r") 
        time.sleep(1) 
        t -= 1

    print('Fire in the hole!!') 

t = 10

countdown(int(t))

【问题讨论】:

    标签: python countdowntimer


    【解决方案1】:

    进口时间

    默认倒计时(t):

    while t: 
        mins, secs, days = divmod(t, 60, 365) 
        timer = '{:02d}:{:02d}:{:02d}'.format(mins, secs, days) 
        print(timer, end="\r") 
        time.sleep(1) 
        t -= 1
    
    print('Fire in the hole!!') 
    

    t = 10

    倒计时(int(t))

    【讨论】:

    • 另外,除以 365 完全没有意义
    【解决方案2】:

    倒计时所做的只是每秒从 t 中减去 1,计时器以正确的格式显示相应的时间。您可以像这样更改格式:

    import time 
    
    def countdown(t): 
    
        while t:
            mins, secs = divmod(t, 60)
            hours, mins = divmod(mins, 60) # divide number of minutes by 60 to get hours
            days, hours = divmod(hours, 24) # divide number of hours by 24 to get days
            timer = '{:02d}:{:02d}:{:02d}:{:02d}'.format(days, hours, mins, secs) 
            print(timer, end="\r") 
            time.sleep(1) 
            t -= 1
    
        print('Fire in the hole!!') 
    
    t = 10
    
    countdown(int(t))
    

    【讨论】:

      【解决方案3】:

      第一件事:

      使用 .sleep() 作为计时器不是一个好主意。 Sleep() 没有考虑运行代码所需的时间。所以你实际上得到的不是一秒钟,而是code execution time + .sleep() + code execution time。这意味着如果将此代码发送给某人,它将根据他们的机器执行代码的速度进行不同的计算。

      如果您有兴趣制作精确的内容,请查看this 和此线程。


      至于我们的问题,您只需要格式化输出:

      def countdown(t): 
          """
          Countdown Timer
          """
          while t:
              # Divmod takes only two arguments so
              # you'll need to do this for each time
              # unit you need to add
              mins, secs = divmod(t, 60) 
              hours, mins = divmod(mins, 60)
              days, hours = divmod(hours, 24)
              timer = '{:02d}:{:02d}:{:02d}:{:02d}'.format(days, hours, mins, secs) 
              print(timer, end="\r") 
              time.sleep(1) 
              t -= 1
      
          print('Fire in the hole!!') 
      
      t = 10
      
      countdown(int(t))
      

      这里是一个小例子,说明如果您测量很长一段时间,这些小差异会叠加起来。

      import time
      
      start_time = time.time()
      
      def countdown(t): 
          """
          Countdown Timer
          """
          while t:
              # Divmod takes only two arguments so
              # you'll need to do this for each time
              # unit you need to add
              mins, secs = divmod(t, 60) 
              hours, mins = divmod(mins, 60)
              days, hours = divmod(hours, 24)
              timer = '{:02d}:{:02d}:{:02d}:{:02d}'.format(days, hours, mins, secs) 
              print(timer, end="\r" )
              time_before_sleep = time.time() - start_time
              time.sleep(1) 
              time_after_sleep = time.time() - start_time
              print(timer, time_before_sleep, time_after_sleep)
              t -= 1
              
          print('Fire in the hole!!') 
      
      t = 10
      
      countdown(int(t))
      

      输出:

      00:00:09 1.0045790672302246 2.0046610832214355
      00:00:08 2.0047121047973633 3.0048279762268066
      00:00:07 3.0049359798431396 4.005050897598267
      00:00:06 4.005151033401489 5.005663156509399
      00:00:05 5.005772113800049 6.006514072418213
      00:00:04 6.006554126739502 7.007684946060181
      00:00:03 7.007725238800049 8.008012056350708
      00:00:02 8.00806212425232 9.00907301902771
      00:00:01 9.009180068969727 10.010266065597534
      

      【讨论】:

      • OP 也要求几天。也许添加days, hours = divmod(hours, 24)
      猜你喜欢
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多