Python Timer Script Example
Build a simple Python timer script that waits for a number of seconds, shows progress, and prints a message when the timer finishes.
This page focuses on a small runnable example, not a full guide to the time module.
Quick example
import time
seconds = 5
print(f"Starting {seconds}-second timer...")
time.sleep(seconds)
print("Time is up!")
This is the simplest timer example. It pauses the program for 5 seconds, then prints a message.
What this example does
This example helps you learn the basic idea behind a timer in Python:
- It shows how to pause a Python program for a set number of seconds
- It uses
time.sleep()to create the delay - It helps beginners understand basic timing scripts
- It keeps the example small and easy to run
If you are new to this, the key idea is simple: Python runs line by line, and time.sleep() tells the program to wait before moving to the next line.
Basic timer script
A basic timer needs only a few steps:
- Import the
timemodule first - Store the timer length in a variable like
seconds - Call
time.sleep(seconds)to wait - Print a message before and after the delay
Here is the full example again:
import time
seconds = 3
print("Timer started")
time.sleep(seconds)
print("Timer finished")
How it works
import timemakes Python load thetimemoduleseconds = 3stores the length of the timertime.sleep(seconds)pauses the program for 3 seconds- The last
print()runs after the pause ends
Expected output
Timer started
Timer finished
You will not see anything during the 3-second wait in this version. If you want visible progress, use a countdown loop.
For more about this function, see time.sleep() explained.
Countdown timer version
A countdown timer is more useful because it shows the remaining time.
This version counts down one second at a time:
import time
seconds = 5
print("Countdown starting...")
for remaining in range(seconds, 0, -1):
print(f"{remaining}...")
time.sleep(1)
print("Time is up!")
How this version works
range(seconds, 0, -1)counts backward- It starts at
seconds - It stops before
0 -1means it moves down by 1 each time
So if seconds is 5, the loop prints:
5...
4...
3...
2...
1...
Then it prints the final message.
Expected output
Countdown starting...
5...
4...
3...
2...
1...
Time is up!
If you want another similar example, see the Python countdown timer example.
Let the user choose the timer length
You can make the script more practical by asking the user for the number of seconds.
Use input() to get text from the user, then convert it to a number.
import time
seconds = int(input("Enter the number of seconds: "))
print(f"Starting {seconds}-second timer...")
time.sleep(seconds)
print("Time is up!")
Why int() is needed
input() always returns a string, even if the user types a number.
For example:
text = input("Enter seconds: ")
print(type(text))
This prints:
<class 'str'>
But time.sleep() needs a number, not a string. That is why int() is used.
If you need help with this step, see how to get user input in Python and how to convert user input to numbers in Python.
Safer version with error handling
If the user types text like hello, int() will fail. This version handles bad input:
import time
try:
seconds = int(input("Enter the number of seconds: "))
if seconds < 0:
print("Please enter 0 or a positive number.")
else:
print(f"Starting {seconds}-second timer...")
time.sleep(seconds)
print("Time is up!")
except ValueError:
print("Please enter a whole number.")
This makes the script easier to use because it does not crash on invalid input.
You can learn more about input() and int().
Common beginner problems
These are some common reasons a timer script does not work as expected.
Forgetting to import time
If you use time.sleep() without importing time, Python will raise an error.
Example:
seconds = 2
time.sleep(seconds)
This fails because time is not defined.
Fix:
import time
seconds = 2
time.sleep(seconds)
Passing a string to time.sleep()
This causes a TypeError.
Example:
import time
seconds = "5"
time.sleep(seconds)
Fix it by converting the value first:
import time
seconds = int("5")
time.sleep(seconds)
Related help: fix TypeError: 'str' object cannot be interpreted as an integer
Invalid text entered instead of a number
This often happens with input().
Example:
seconds = int(input("Enter seconds: "))
If the user types abc, Python raises a ValueError.
Fix it with try and except, as shown earlier.
Related help: fix ValueError: invalid literal for int() with base 10
Negative values are not useful for a timer
A negative timer does not make sense for most beginner scripts.
Check for this before running the timer:
seconds = int(input("Enter seconds: "))
if seconds < 0:
print("Seconds cannot be negative.")
else:
print("Valid timer value")
Wrong range() values in a countdown loop
If the countdown does not work, check the loop carefully.
Correct version:
for remaining in range(seconds, 0, -1):
print(remaining)
A common mistake is using the wrong start, stop, or step value.
Expecting sleep() to show progress by itself
time.sleep() only pauses the program. It does not display progress.
If you want visible updates, you need a loop:
import time
for i in range(3, 0, -1):
print(i)
time.sleep(1)
Useful debugging commands
These can help when your timer is not working:
python timer.py
print(type(seconds))
print(seconds)
help(time.sleep)
These checks can help you confirm:
- Whether
secondsis really a number - What value it contains
- How
time.sleep()is supposed to work
How to improve this script
Once the basic version works, you can build on it.
Some useful improvements are:
- Add input validation with
try-except - Display minutes and seconds instead of only seconds
- Play a sound or print a stronger alert at the end
- Turn the timer code into a function for reuse
Here is one small improvement: turning the timer into a function.
import time
def start_timer(seconds):
print(f"Starting {seconds}-second timer...")
time.sleep(seconds)
print("Time is up!")
start_timer(4)
This is useful when you want to reuse the same logic more than once in a program.
If you want a broader overview, see the Python time module overview.
FAQ
How do I make a countdown timer in Python?
Use a loop that prints the remaining seconds and pauses for 1 second with time.sleep(1).
Why does my timer not wait?
Check that you are calling time.sleep() with a number and that the code is actually being reached.
Why does input() not work with time.sleep()?
input() returns a string. Convert it first with int() or float() before passing it to sleep().
Can time.sleep() use decimal values?
Yes. You can use values like 0.5 or 1.5 for fractions of a second.
See also
- Python time module overview
time.sleep()explained- Python
input()function explained - Python
int()function explained - How to convert user input to numbers in Python
- Python countdown timer example
Try changing the timer so the user chooses the number of seconds, then turn it into a countdown script. That is a good next step after this basic example.