Create a Countdown time in Python
In this example, we will create a countdown timer in python. The program will ask the user to input time. Then the program will set the countdown from that time given by the user. To understand this program you should have basic knowledge of -
- Modules in Python
- Defining a function
- While loop in Python
Code:
import time
def countdown(time_sec):
while time_sec:
mins, secs = divmod(time_sec, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
print(timeformat, end='\r')
time.sleep(1)
time_sec -= 1
print("stop")
timer = int(input("Enter countdown: "))
countdown(timer)
Tags:
Advanced project