Countdown timer code
import time
def countdown(seconds):
print(f"--- Countdown Timer: {seconds} seconds ---")
while seconds:
mins, secs = divmod(seconds, 60)
time_format = f"{mins:02d}:{secs:02d}"
print(time_format, end='\r') # overwrite the line
time.sleep(1)
seconds -= 1
print("00:00\n⏰ Time's up!")
def main():
try:
total_seconds = int(input("Enter countdown time in seconds: "))
if total_seconds <= 0:
print("Please enter a positive number.")
return
countdown(total_seconds)
except ValueError:
print("Invalid input. Please enter an integer.")
main()
Code output
Enter countdown time in seconds: 10
00:10
00:09
...
00:00
⏰ Time's up!