【问题标题】:Timer implementation in pythonpython中的定时器实现
【发布时间】:2022-08-03 18:44:30
【问题描述】:

我正在尝试实现一个倒计时小时、分钟和秒的计时器。我在互联网上看到了类似的实现,但仍然没有打印到终端:


import time

time_to_wait = 30

while time_to_wait:
   seconds = time_to_wait % 60
   mins = time_to_wait // 60
   hours = mins * 60
   timer = \'{:02d}:{:02d}:{:02d}\'.format(hours, mins, seconds)
   print(timer, end=\"\\r\")
   time.sleep(1)
   time_to_wait -= 1

  • 该代码对我有用,即打印时间。你如何运行它?

标签: python timer


【解决方案1】:
print(timer, end="\r")

\r 表示回车,因此打印计时器然后(非常快速)擦除,以避免您应该先打印回车,即请尝试使用

print("\r", timer, end="")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多