Create count down timer using Python

Step 1: import the time module.

The time module in the python provides function provides for working with time related tasks.

import time

Step 2: Define the countdown function.

Create a function that takes the total number of second as an argument and count down from that value.

def countdown(t):

Step 3:  A while loop in this function continues to execute  until time equals 0.

Step 4:  Calculate the duration in minutes and seconds using divmod().

mins, sec = divmod(t, 60)

Step 5:  Using the variable time format, output the minutes and second just on display.

timer ='{:02d}:{:02d}'.format(mins,sec)

Step 6:  By using end = t to drive the pointer to the beginning of the display.

Step 7:  The code pause for a second, use the time.sleep( ) function.

time.sleep(1)

Step  8:  The end of a countdown, we will print “End of  Time” after the loop has finished

Programming :-

#import the time module.
import time

#define the countdown function.
def countdown(t):
#while loop.
while t:
mins, sec =divmod(t,60)
timer ="{:02d} : {:02d}".format(mins,sec)
print(timer,end = "\n")
time.sleep(1)
t-=1
print("End of timer")

#input time in seconds.
t=input("Enter the time in second :")

#function call
countdown(int(t))

Output :-

Enter the time in second :5

00:05

00:04

00:03

00:02

00:01

End of  timer

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top