To create a Python countdown timer, you can use the time
module and a loop that decrements a counter until it reaches 0. Here’s an example:
import time
seconds = 10
while seconds > 0:
print(seconds)
time.sleep(1)
seconds -= 1
print("Time's up!")
In this example, we define a variable seconds
that represents the duration of the countdown in seconds. We use a while
loop to repeatedly print the remaining number of seconds and wait for 1 second using the time.sleep()
function. We also decrement the seconds
variable by 1 in each iteration of the loop.
When the seconds
variable reaches 0, the loop terminates and the final message is printed.
You can modify this example to perform some action when the countdown reaches 0, such as playing a sound or sending a notification.
+ There are no comments
Add yours