Python Program to Create a Countdown Timer
To understand this example, you should have the knowledge of the following Python programming topics:
Countdown time in Python
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")
countdown(5)
- The
divmod()method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder. end='\r'overwrites the output for each iteration.- The value of
time_secis decremented at the end of each iteration.
Also Read:
Your builder path starts here. Builders don't just know how to code, they create solutions that matter.
Escape tutorial hell and ship real projects.
- Real-World Projects
- On-Demand Learning
- AI Mentor
- Builder Community