22

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! \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.

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

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

trevor4n
  • 281
  • 1
  • 3
  • 11
  • Do you want it in `MM:SS` format or just `3...2...1`? You've mentioned both. – TheSoundDefense Aug 07 '14 at 18:29
  • I want it in MM:SS but I was just showing what I had started with @TheSoundDefense – trevor4n Aug 07 '14 at 18:30
  • `3...2...1...0...Goodbye!` will apprear all together at the end of the loop.
    use flush to "force" the print without waiting for `\r` or `\n`: `sys.stdout.write(str(t)+'...')` `sys.stdout.flush()`
    – luney Jun 01 '19 at 23:38

5 Answers5

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
3

Here is the code which counts from 01:05 to 00:00 in MM:SS format.

Python 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 :

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)
Srivishnu
  • 759
  • 1
  • 6
  • 15
1

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.

zemunkh
  • 662
  • 7
  • 13
1

Maybe this link will help: Making a Timer in 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!
UltraStudioLTD
  • 300
  • 2
  • 14
-1
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)
adam gay
  • 1
  • 1
  • It would be most helpfull if you could provide some information why OP's code is not working with some explanation. – MaLiN2223 Feb 05 '17 at 21:15