Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Create a Lap Timer
When it is required to create a lap timer using Python, the time module is used. The program tracks individual lap times and cumulative time using keyboard interrupts to control the timer flow.
A lap timer records the duration of each lap in a sequence, commonly used in sports timing or performance tracking applications.
Example
import time
start_time = time.time()
end_time = start_time
lap_num = 1
print("Click on ENTER to count laps.\nPress CTRL+C to stop")
try:
while True:
input()
time_laps = round((time.time() - end_time), 2)
tot_time = round((time.time() - start_time), 2)
print("Lap No. " + str(lap_num))
print("Total Time: " + str(tot_time))
print("Lap Time: " + str(time_laps))
print("*" * 20)
end_time = time.time()
lap_num += 1
except KeyboardInterrupt:
print("Exit!")
Output
Click on ENTER to count laps. Press CTRL+C to stop Lap No. 1 Total Time: 1.77 Lap Time: 1.77 ******************** Lap No. 2 Total Time: 3.52 Lap Time: 1.75 ******************** Exit!
How It Works
The program uses three key time measurements ?
start_time ? Records when the timer begins
end_time ? Updates after each lap to calculate individual lap duration
current time ? Retrieved using
time.time()for calculations
Key Components
The lap timer consists of several important elements ?
Interactive Input ?
input()waits for user to press EnterTime Calculation ? Lap time = current time - previous lap end time
Total Time ? Cumulative time = current time - start time
Exception Handling ?
KeyboardInterruptcatches Ctrl+C to exit gracefully
Enhanced Version
import time
def lap_timer():
start_time = time.time()
end_time = start_time
lap_num = 1
lap_times = []
print("=== LAP TIMER ===")
print("Press ENTER to record each lap")
print("Press CTRL+C to stop and show summary\n")
try:
while True:
input(f"Press ENTER for Lap {lap_num}: ")
current_time = time.time()
lap_time = round(current_time - end_time, 2)
total_time = round(current_time - start_time, 2)
lap_times.append(lap_time)
print(f"Lap {lap_num}: {lap_time}s (Total: {total_time}s)")
print("-" * 30)
end_time = current_time
lap_num += 1
except KeyboardInterrupt:
print(f"\n=== TIMER STOPPED ===")
print(f"Total Laps: {len(lap_times)}")
if lap_times:
print(f"Fastest Lap: {min(lap_times)}s")
print(f"Average Lap: {round(sum(lap_times)/len(lap_times), 2)}s")
# Run the timer
lap_timer()
=== LAP TIMER === Press ENTER to record each lap Press CTRL+C to stop and show summary Press ENTER for Lap 1: Lap 1: 2.15s (Total: 2.15s) ------------------------------ Press ENTER for Lap 2: Lap 2: 1.89s (Total: 4.04s) ------------------------------ === TIMER STOPPED === Total Laps: 2 Fastest Lap: 1.89s Average Lap: 2.02s
Conclusion
The lap timer uses time.time() to measure intervals and KeyboardInterrupt for graceful termination. This creates an interactive timing system perfect for tracking performance across multiple laps or iterations.
