Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

python - Countdown Clock: 01:05

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 actual second then the old timer should be replaced on its line with a new timer that is one second lower: 01:00 becomes 00:59 and it actually hits 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! 
 
 
 
 
')

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.

RESULT: 3...2...1...0...Goodbye!

I know this is similar to other countdown questions but I believe that it has its own twist.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Apart from formatting your time as minutes and seconds, you'll need to print a carriage return. Set end to :

import time

def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        print(timeformat, end='
')
        time.sleep(1)
        t -= 1
    print('Goodbye!




')

This ensures that the next print overwrites the last line printed:

countdown


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...