Python 倒计时:01:05
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25189554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Countdown Clock: 01:05
提问by trev
How can I create a countdown clock in Python that looks like 00:00 (min & sec) which is on a line of its own. Every time it decreases by one actualsecond then the old timer should be replaced on its line with a new timer that is one second lower:
01:00becomes 00:59and it actually hits 00:00.
如何在 Python 中创建一个倒计时时钟,它看起来像 00:00(分钟和秒),它在自己的一条线上。每次实际减少一秒时,旧计时器应在其行上替换为低一秒的新计时器:
01:00变成00:59并且实际上命中00:00。
Here is a basic timer I started with but want to transform:
这是我开始使用但想要转换的基本计时器:
def countdown(t):
import time
print('This window will remain open for 3 more seconds...')
while t >= 0:
print(t, end='...')
time.sleep(1)
t -= 1
print('Goodbye! \n \n \n \n \n')
t=3
I also want to make sure that anything after Goodbye!(which would most likely be outside of the function) will be on its own line.
我还想确保之后的任何内容Goodbye!(很可能在函数之外)都将在自己的行上。
RESULT:3...2...1...0...Goodbye!
结果:3...2...1...0...Goodbye!
I know this is similar to other countdown questions but I believe that it has its own twist.
我知道这与其他倒计时问题类似,但我相信它有其独特之处。
采纳答案by Martijn Pieters
Apart from formatting your time as minutes and seconds, you'll need to print a carriage return. Set endto \r:
除了将您的时间格式化为分钟和秒之外,您还需要打印一个回车符。设置end为\r:
import time
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
print(timeformat, end='\r')
time.sleep(1)
t -= 1
print('Goodbye!\n\n\n\n\n')
This ensures that the nextprint overwrites the last line printed:
这确保下一次打印覆盖最后一行打印:


回答by Srivishnu
Here is the code which counts from 01:05 to 00:00 in MM:SS format.
这是从 01:05 到 00:00 以 MM:SS 格式计数的代码。
Python 3 :
蟒蛇3:
import time
def countdown(p,q):
i=p
j=q
k=0
while True:
if(j==-1):
j=59
i -=1
if(j > 9):
print(str(k)+str(i)+":"+str(j), end="\r")
else:
print(str(k)+str(i)+":"+str(k)+str(j), end="\r")
time.sleep(1)
j -= 1
if(i==0 and j==-1):
break
if(i==0 and j==-1):
print("Goodbye!", end="\r")
time.sleep(1)
countdown(1,5) #countdown(min,sec)
Python 2 :
蟒蛇2:
import time
def countdown(p,q):
i=p
j=q
k=0
while True:
if(j==-1):
j=59
i -=1
if(j > 9):
print "\r"+str(k)+str(i)+":"+str(j),
else:
print "\r"+str(k)+str(i)+":"+str(k)+str(j),
time.sleep(1)
j -= 1
if(i==0 and j==-1):
break
if(i==0 and j==-1):
print "\rGoodbye!"
time.sleep(1)
countdown(1,5) #countdown(min,sec)
回答by adam gay
import time
import sys
print(' ')
print('Countdown Timer, By Adam Gay')
print(' ')
print('Instructions: Input time to countdown from.')
print(' ')
c=':'
hourz=input('Hours: ')
minz=input('Minutes: ')
secz=input('Seconds: ')
print(' ')
hour=int(hourz)
min=int(minz)
sec=int(secz)
while hour > -1:
while min > -1:
while sec > 0:
sec=sec-1
time.sleep(1)
sec1 = ('%02.f' % sec) # format
min1 = ('%02.f' % min)
hour1 = ('%02.f' % hour)
sys.stdout.write('\r' + str(hour1) + c + str(min1) + c + str(sec1))
min=min-1
sec=60
hour=hour-1
min=59
Print('Countdown Complete.')
time.sleep(30)
回答by zemunkh
For the simplicity, this code is able to say you how long it takes until the next desired time, which might be whatever you want to do in your program. In your case, this is a kind of countdown timer.
为简单起见,此代码能够告诉您下一个所需时间需要多长时间,这可能是您想在程序中执行的任何操作。在您的情况下,这是一种倒数计时器。
from datetime import datetime
x=datetime.today()
y=x.replace(day=x.day+1, hour=3, minute=1, second=0, microsecond=0)
delta_t=y-x
secs=delta_t.seconds+1
second = (secs % 60)
minut = (secs / 60) % 60
hour = (secs / 3600)
print ("Seconds: %s " % (second))
print ("Minute: %s " % (minut))
print ("Hour: %s" % (hour))
print ("Time is %s:%s:%s" % (hour, minut, second))
Then, output is as follows:
然后,输出如下:
Seconds: 50
Minute: 32
Hour: 12
Time is 12:32:50
Good luck with your coding.
祝你编码好运。
回答by Ultra Gamer
Maybe this link will help: Making a Timer in Python 3
也许此链接会有所帮助: 在 Python 3 中制作计时器
And look at my answer, it is same for your too!
看看我的回答,你也一样!
Anyway, here is answer:
无论如何,这是答案:
import time
import os
hour = int(input('Enter any amount of hours you want -+==> '))
minute = int(input('Enter any amount of minutes you want -+==> '))
second = int(input('Enter any amount of seconds you want -+==> '))
time = hour*10800 + minute*3600 + second*60
print('{}:{}:{}'.format(hour,minute,second))
while time > 0:
time = time - 1
seconds = (time // 60) % 60
minutes = (time // 3600)
hours = (time // 10800)
print('Time Left -+==> ',hours,':',minutes,':',seconds,)
os.system("CLS")
if time == 0:
print('Time Is Over!')
Input:
输入:
Enter any amount of hours you want -+==> 0
Enter any amount of minutes you want -+==> 0
Enter any amount of seconds you want -+==> 10
Output # All are on the same line
输出 # 都在同一行
Time Left -+==> 0:0:10
Time Left -+==> 0:0:9
Time Left -+==> 0:0:8
Time Left -+==> 0:0:7
Time Left -+==> 0:0:6
Time Left -+==> 0:0:5
Time Left -+==> 0:0:4
Time Left -+==> 0:0:3
Time Left -+==> 0:0:2
Time Left -+==> 0:0:1
Time Left -+==> 0:0:0
Time Is Over!

