Python Program to Create a Lap Timer

Python Program to Create a Lap Timer: In this article, we will make a simple timer to calculate lap-time intervals using Python. Module used time:  This module provides various time-related functions. It is a part of Python’s standard library and does not require installation.

The lap timer requires a fixed position on the circuit from which he can trigger lap after lap with accuracy and reliability. In this tutorial, we will Let us consider the input-output for a better understanding:In the above figure, we can see that there is info to start and stop the counter and when you will enter the number it started counting the laps until you press the CTRL+C to stop the lap.

Approach Create a Lap Timer


The user needs to press ENTER to complete each lap. The timer keeps counting till CTRL+C is pressed. For each lap we calculate the lap time by subtracting the current time from the total time at the end of the previous lap. The time() function of the time module, returns the current epoch time in milliseconds.

Algorithm Create a Lap Timer

As of now, we have a rough understanding of how the following program will execute. For a better understanding let us dive deep into the algorithm followed by the program.

  1. Import library “time”
  2. Define start, last, and num for the timer to start
  3. Use try-catch block and Input() for the enter key
  4. Calculate the current lap time
  5. Calculate the time elapsed
  6. Print lap number, total time and lap-time
  7. Update previous total time and lap number
  8. Stop when CTRL+C is pressed

Below is the implementation:

# importing libraries
import time


# Timer starts
starttime=time.time()
lasttime=starttime
lapnum=1

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

try:
	while True:
			
		# Input for the ENTER key press
		input()

		# The current lap-time
		laptime=round((time.time() - lasttime), 2)

		# Total time elapsed
		# since the timer started
		totaltime=round((time.time() - starttime), 2)

		# Printing the lap number,
		# lap-time and total time
		print("Lap No. "+str(lapnum))
		print("Total Time: "+str(totaltime))
		print("Lap Time: "+str(laptime))
			
		print("*"*20)

		# Updating the previous total time
		# and lap number
		lasttime=time.time()
		lapnum+=1

# Stopping when CTRL+C is pressed
except KeyboardInterrupt:
	print("Done")

Output:

ENTER to count laps.
Press CTRL+C to stop

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

Lap No. 2
Total Time: 2.66
Lap Time: 1.41
********************

Lap No. 3
Total Time: 5.06
Lap Time: 2.23
********************

Lap No. 4
Total Time: 5.63
Lap Time: 0.4
********************
Done

Python Program

As mentioned in the algorithm let’s try the same steps into the program to get the desired

output:

import time
start=time.time()
last=start
num=1

print("Press ENTER to count lap timer.\nPress CTRL+C to stop")
try:
	while True:
		input()
		lap=round((time.time() - last), 2)
		total=round((time.time() - start), 2)
		print("Lap Numer "+str(num))
		print("Total Time taken: "+str(total))
		print("Lap Time: "+str(lap))			
		print("*"*20)
		last=time.time()
		num+=1

# Stop
except KeyboardInterrupt:
	print("Process Stopped")

out put

Press ENTER to count lap timer.
Press CTRL+C to stop
1
Lap Numer 1
Total Time taken: 1.71
Lap Time: 1.71
********************
2
Lap Numer 2
Total Time taken: 3.36
Lap Time: 1.64
********************
3
Lap Numer 3
Total Time taken: 4.86
Lap Time: 1.5
********************
Process Stopped

Conclusion

In this tutorial, we have created a lap timer with help of a python code. We have imported the time module and uses try and catch block to create a lap timer. When the user presses Enter key the timer starts until CTRL+C is pressed by the user.

Leave a Reply