0% found this document useful (0 votes)
3 views5 pages

Python Program lap-WPS Office

The document outlines how to create a simple lap timer in Python using two methods: 'time.perf_counter()' for high-resolution timing and 'time.time()' for measuring elapsed time since the epoch. The timer records lap times each time the user presses the ENTER key and continues until stopped with CTRL+C. It includes example code for both methods and explains key components such as lap time calculation and total elapsed time.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Program lap-WPS Office

The document outlines how to create a simple lap timer in Python using two methods: 'time.perf_counter()' for high-resolution timing and 'time.time()' for measuring elapsed time since the epoch. The timer records lap times each time the user presses the ENTER key and continues until stopped with CTRL+C. It includes example code for both methods and explains key components such as lap time calculation and total elapsed time.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Program to Create a Lap Timer

Last Updated : 4 Nov, 2025

Task is to build a simple lap timer in Python that records and displays the time for each lap and the total
elapsed time. The timer starts immediately, and each time the user presses the ENTER key, a new lap
time is recorded. The timer continues running until the user stops it manually using CTRL+C.

Let’s explore different methods to create a lap timer in Python.

Using time.perf_counter()

This method uses the "time.perf_counter()" function, which provides high-resolution timing and is more
accurate for measuring small intervals. It continuously tracks time and prints lap and total durations
every time the user presses ENTER.

import time

s = time.perf_counter()

l=s

lap = 1

print("Press ENTER to record laps.\nPress CTRL+C to stop")

try:

while True:

# wait for ENTER key press

input()

# calculate lap time and total time

cur = time.perf_counter()

lt = round(cur - l, 4)
tt = round(cur - s, 4)

print("Lap No.:", lap)

print("Total Time:", tt)

print("Lap Time:", lt)

print("*" * 20)

# update previous lap time

l = cur

lap += 1

# stop when CTRL+C is pressed

except KeyboardInterrupt:

print("Done")

Explanation:

time.perf_counter(): Provides a high-resolution timer to measure elapsed time accurately.

s and l: Store the start time and last lap time respectively.

lap: Tracks the current lap number.

input(): Waits for the user to press ENTER to record the next lap.

Lap Time (lt): Difference between the current and previous lap time.

Total Time (tt): Time elapsed since the stopwatch started.

round(): Rounds the time values to 4 decimal places for readability.

while loop continuously records laps until the user interrupts using CTRL+C.

KeyboardInterrupt block: Gracefully ends the stopwatch and prints "Done".


Using [Link]()

This method uses Python’s built-in time module to measure elapsed time between laps. The [Link]()
function returns the current time in seconds since the epoch. Each time the user presses ENTER, it
calculates the lap time and total time by subtracting previous timestamps.

import time

st = [Link]()

lt = st

lapnum = 1

print("Press ENTER to count laps.\nPress CTRL+C to stop")

try:

while True:

input()

laptime = round(([Link]() - lt), 2)

tt = round(([Link]() - st), 2)

print("Lap No.", lapnum)

print("Total Time:", tt)

print("Lap Time:", laptime)

print("*" * 20)

lt = [Link]()

lapnum += 1
except KeyboardInterrupt:

print("Done")

Output

Press ENTER to count laps.

Press CTRL+C to stop

Lap No. 1

Total Time: 3.05

Lap Time: 3.05

********************

Lap No. 2

Total Time: 4.33

Lap Time: 1.28

********************

Lap No. 3

Total Time: 5.26

Lap Time: 0.93

********************

Done

Explanation:
st = [Link]() records the starting time when the program begins and lt = st keeps track of the
previous lap’s end time.

lapnum = 1 initializes the first lap number and [Link]() returns the current system time in seconds.

round(value, 2) rounds the time to 2 decimal places for readability.

while True: keeps the timer running continuously until stopped manually and KeyboardInterrupt stops
the program when the user presses CTRL+C.

You might also like