How to Create a Countdown Timer in Python

81 views

How to Create a Countdown Timer in Python

How to Create a Countdown Timer in Python

I want to create a countdown timer that counts down from a specified number of seconds. How can I do this in Python?

solveurit24@gmail.com Changed status to publish February 16, 2025
0

You can create a countdown timer using a loop and the time.sleep() function to pause execution.

Code Example:

import time

def countdown(seconds):
    while seconds > 0:
        print(f"Time remaining: {seconds} seconds")
        time.sleep(1)
        seconds -= 1
    print("Time's up!")

countdown(5)

This function starts at the specified number of seconds and decrements by 1 each second, printing the remaining time. When it reaches 0, it prints “Time’s up!”.

solveurit24@gmail.com Changed status to publish February 16, 2025
0