This Python script lets users set their own countdown duration by entering the number of seconds. It validates the input and displays a live timer until it reaches zero, announcing when time is up.
Click to view script…
import time
def countdown(seconds):
while seconds:
mins, secs = divmod(seconds, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
seconds -= 1
print("Time's up!")
try:
user_input = int(input("Enter the number of seconds for the countdown: "))
countdown(user_input)
except ValueError:
print("Invalid input. Please enter an integer value.")