【问题标题】:How to add +1 to a variable for every X minutes past in the timer in the code如何在代码中的计时器中每过去 X 分钟向变量添加 +1
【发布时间】:2020-05-06 01:45:49
【问题描述】:

我有 3 个变量(ADG、DR、TC),我想分别计算每个 45 分钟和 2.5 小时通过的每个 3 个变量。我有这段代码来创建一个 24 小时计时器,我需要将这些变量(及其计数)添加到 while when_to_stop > 0: 循环中。有什么建议吗?谢谢!

import time

while True:
    uin = input(">>")
    try:
        when_to_stop = abs(int(uin))
    except KeyboardInterrupt:
        break
    except:
        print("Not a number!")

    while when_to_stop > 0:
        m, s = divmod(when_to_stop, 60)
        h, m = divmod(m, 60)
        time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
        print(time_left + "\r", end="" )
        time.sleep(0.001)
        when_to_stop -= 1

        # ADG - Auto Droplet Generator (45 mins)
        # DR - Droplet Reader (time same as TC)
        # TC - Ther mal cycler  (2.5 hrs)

"""

【问题讨论】:

    标签: time while-loop count counter


    【解决方案1】:

    添加了经过的秒数计数器。

    当经过的秒数可以被 (45 * 60 秒) 整除时,45 分钟过去了,每次发生这种情况时,ADG 变量都会加一。

    当经过的秒数可以被 (150 * 60 秒) 整除时,2.5 小时过去了,每次发生这种情况时,TC 变量都会加一。 DR 变量的作用方式相同(时间与 TC 相同),因此它与 TC 变量具有相同的值。

    import time
    elapsed = 0 
    ADG = 0
    DR = 0
    TC = 0
    
    while True:
        uin = input(">>")
        try:
            when_to_stop = abs(int(uin))
        except KeyboardInterrupt:
            break
        except:
            print("Not a number!")
    
        while when_to_stop > 0:
            m, s = divmod(when_to_stop, 60)
            h, m = divmod(m, 60)
            time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
            print(time_left + "\r", end="" )
            time.sleep(0.001)
            when_to_stop -= 1
            elapsed += 1
    
            #Auto Droplet Generator (45 mins)
            if (elapsed % (45*60) == 0):
                ADG += 1 
            # DR - Droplet Reader (time same as TC)
            if (elapsed % (150*60) == 0):
                DR += 1        
            # TC - Ther mal cycler  (2.5 hrs)
            if (elapsed % (150*60) == 0):
                TC += 1 
    

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 2012-04-21
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      相关资源
      最近更新 更多