Python 日期、小时、分钟和秒的倒数计时器
Python count down timer for date, hour, minute and second
在 this link 的第二个示例中,我找到了一个倒数计时器,但它只能工作几分钟和几秒钟,我怎样才能让它工作数小时和数天。这是代码
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))
导入时间
默认倒计时(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
倒计时(整数(t))
倒计时所做的就是每秒从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))
要事第一:
将 .sleep() 用作计时器不是一个好主意。 Sleep() 没有考虑 运行 代码所需的时间。所以你实际上得到的不是一秒钟,而是 code execution time
+ .sleep()
+ code execution 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.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
在 this link 的第二个示例中,我找到了一个倒数计时器,但它只能工作几分钟和几秒钟,我怎样才能让它工作数小时和数天。这是代码
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))
导入时间
默认倒计时(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
倒计时(整数(t))
倒计时所做的就是每秒从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))
要事第一:
将 .sleep() 用作计时器不是一个好主意。 Sleep() 没有考虑 运行 代码所需的时间。所以你实际上得到的不是一秒钟,而是 code execution time
+ .sleep()
+ code execution 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.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