Countdown Clock: 01:05

后端 未结 5 2142
长情又很酷
长情又很酷 2020-12-13 07:31

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

5条回答
  •  执笔经年
    2020-12-13 07:59

    Apart from formatting your time as minutes and seconds, you'll need to print a carriage return. Set end to \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 next print overwrites the last line printed:

    countdown

提交回复
热议问题