Python Program to Create a Lap Timer (original) (raw)

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.

Python `

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")

`

**Output

Press ENTER to record laps.
Press CTRL+C to stop

Lap No.: 1
Total Time: 1.9188
Lap Time: 1.9188
********************

Lap No.: 2
Total Time: 3.5742
Lap Time: 1.6555
********************

Lap No.: 3
Total Time: 4.1417
Lap Time: 0.5675
********************
Done

**Explanation:

Using time.time()

This method uses Python’s built-in time module to measure elapsed time between laps. The time.time() 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.

Python `

import time st = time.time() lt = st lapnum = 1

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

try: while True: input()

    laptime = round((time.time() - lt), 2)
    tt = round((time.time() - st), 2)

    print("Lap No.", lapnum)
    print("Total Time:", tt)
    print("Lap Time:", laptime)
    print("*" * 20)

    lt = time.time()
    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: