Create a Countdown Timer in Python

How to Create a Countdown Timer in Python

# Create a Countdown Timer in Python 


# import the time module
import time

# define the countdown func.
def countdown(t):
	
	while t:
		mins, secs = divmod(t, 60)
		timer = '{:02d}:{:02d}'.format(mins, secs)
		print(timer, end="\r")
		time.sleep(1)
		t -= 1
	
	print('time up!!')


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

# function call
countdown(int(t))

Output:

Enter the time value in seconds: 
15
00:15
00:14
00:13
00:12
00:11
00:10
00:09
00:08
00:07
00:06
00:05
00:04
00:03
00:02
00:01
time up!!

About The Author

Leave a Reply