- 1 year ago
How to make a timer that counts down from 30 by 1 every second?
You can create a timer in Python using the time
module and a loop. Here's an example of a timer that counts down from 30 to 0 by 1 every second:
pythonimport time
def countdown_timer(seconds):
while seconds > 0:
print(seconds)
time.sleep(1)
seconds -= 1
print("Time's up!")
countdown_timer(30)
In this example, the countdown_timer
function takes the initial number of seconds as a parameter. It enters a loop that continues until the seconds
variable reaches 0. Inside the loop, it prints the current value of seconds
, waits for 1 second using time.sleep(1)
, and then decrements the seconds
variable by 1.
You can adjust the initial number of seconds (countdown_timer(30)
) to any desired value.
When the countdown reaches 0, it will print "Time's up!".
Feel free to modify the code according to your specific requirements, such as adding additional functionality or incorporating it into a larger program.