【问题标题】:Creating a counter which resets itself every 30 seconds创建一个每 30 秒重置一次的计数器
【发布时间】:2019-12-07 22:12:47
【问题描述】:

我想创建一个计数到 30 的函数,但是当它达到 30 时,我想将其重置为起点。

def countdown():
  global countDown
  countDown = int(time.time() - start_time)
  return countDown % 30

然后我想像那样打印它。

print("You have " + str(30 - countdown()) + " time") 

它可以工作,但是当它达到 0 时,它会像 -1,-2 一样一直在 0 以下计数,并且它没有进行模运算。所以它不会自行重置。在这种情况下我该怎么办?

所需情况:30 29.... 3 2 1 0 30 29 28 近期案例:30 29 ... 2 1 0 -1 -2

【问题讨论】:

  • 当你说“不工作”是什么意思?你有错误吗?它是否运行但没有达到您的预期?
  • 它可以工作,但是当它达到 0 时,它会像 -1,-2 一样一直在 0 以下计数,并且它没有进行模运算。所以它不会自行重置。
  • 使用添加的代码import timestart_time = time.time() 无法重现
  • 你不应该重置,这就是重点。您只设置了一次start_time,由于模运算,它将重新开始。
  • “时间为浮点数,以自纪元以​​来的秒数表示,以 UTC 为单位”,因此在午夜前后甚至都不是问题。获得模

标签: python python-3.x counter


【解决方案1】:

未使用模运算符 (countDown % 30) 重置计数器。试试吧,

import time
def countdown(i):
  counter = i
  while True:
    if (counter == i):
      counter = 0
    print(counter)
    counter = counter + 1
    time.sleep(1)

countdown(30)

【讨论】:

  • 我不能使用 time.sleep() 函数,因为我在循环中遇到了很多问题。我无法中断输入操作。
【解决方案2】:

我从你的代码中得到了什么

import time
start_time = time.time()
def countdown():
  global countDown
  countDown = int(time.time() - start_time)
  return countDown % 30

print("You have " + str(30 - countdown()) + " time")

https://www.python.org/shell/上运行良好
无法重现您的问题。或者不是你问题中的代码!

【讨论】:

  • 我无法分享我的所有代码,这就是为什么我无法充分解释自己的原因。我会给一些代码输入,当我这样做时,它会做一些东西并告诉我还剩多少时间。我们有 30 秒,当它变为 0 时,第二个东西会来,计数器会重新开始计数。
【解决方案3】:

尽量避免使用全局变量。此外,使用 4 个空格缩进。 我会使用时间长度作为输入。

from time import time

def do_something():
    pass

def get_time(start_time):
    # returns how much time did it pass from event that started at start_time
    return time() - start_time

def countdown(countDown):
    start_time = time()
    # this is counter that attains consecutive values [0, 1, ..., countDown]
    current_count = 0
    while current_count < countDown:
        print(countDown - current_count, end=' ')
        while get_time(start_time) - current_count < 1:
            do_something()
            #warning: if do_something takes significant anount of
            #time forthcoming print won't be correct
        current_count += 1
    print(countDown - current_count, end=' ')
    return current_count

countdown(7)
countdown(5)

也是目的

print("You have " + str(30 - countdown()) + " time") 

我不清楚。在脚本中的任何位置使用它。

【讨论】:

    【解决方案4】:

    由于问题非常不清楚,因此很难准确地创建您正在寻找的内容。但是,无论您打算如何使用此代码,它都应该适合您。

    此代码允许您:

    • 做一个计时器
    • 抓紧时间
    • 在计时器倒计时时运行代码
    • 定时器结束后运行代码。
    • 您还可以重置计时器

    代码:

    import time
    
    class countdown():
        def start(self):
            self.t = time.time()
        def remaining(self):
            return 30 - int(time.time()-self.t)
    
    
    timer = countdown()
    timer.start()
    while True:
        print(30 - countdown(), "seconds remaining") #Still time left code
        if timer.remaining() <= 0:
            pass #30 seconds over code
            timer.reset() #Starts timer again
    

    【讨论】:

      【解决方案5】:

      正如其他人指出的那样,您的功能不是重置您的计数器。 试试下面的小修改:

      def countdown():
        global countDown
        countDown = int(time.time() - start_time) % 30
        return countDown
      

      【讨论】:

        【解决方案6】:

        首先,我想向大家道歉,因为这是我的第一个问题,所以没有清楚地解释我的问题。其次,如示例所示,我看到我的代码没有犯任何错误。第一部分是正确的,但是当它打印 ("You have " + str(5 - countdown()) + " time") 时向我显示负数,因为我采用 30 的模数,但在 print func "5 - countdown()" 中显示它。因此,当它变为 15 时,它将返回 15 但 5 - 15 = -10。感谢所有试图提供帮助的人。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-06
          • 2021-12-03
          • 1970-01-01
          • 1970-01-01
          • 2016-11-07
          相关资源
          最近更新 更多