How to Create a Lap Timer in Python

In this tutorial, we will program 'How to Create a Lap Timer in Python.' We will learn how to display a lap timer in the console terminal. The objective is to create a lap timer program. I will provide a sample program to demonstrate the actual coding process in this tutorial.

This topic is very easy to understand. Just follow the instructions I provide, and you will be able to do it yourself with ease. The program I will show you covers the basics of programming for creating a lap timer. I will do my best to provide you with a simple method for starting a lap timer. So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import time
  2.  
  3.  
  4. starttime = time.time()
  5. lasttime = starttime
  6. lapnum = 1
  7.  
  8. ret = False
  9.  
  10.  
  11. while True:
  12.         print("\n================== Lap Timer ==================\n\n")
  13.  
  14.  
  15.         print("Press ENTER to count laps.\nPress CTRL+C to stop")
  16.  
  17.         try:
  18.                 while True:
  19.  
  20.                         input()
  21.  
  22.                         laptime = round((time.time() - lasttime), 2)
  23.  
  24.        
  25.                         totaltime = round((time.time() - starttime), 2)
  26.  
  27.                
  28.                         print("Lap No. "+str(lapnum))
  29.                         print("Total Time: "+str(totaltime))
  30.                         print("Lap Time: "+str(laptime))
  31.  
  32.                         print("*"*20)
  33.  
  34.        
  35.                         lasttime = time.time()
  36.                         lapnum += 1
  37.  
  38.         except KeyboardInterrupt:
  39.                 print("Done")
  40.  
  41.         opt = input("\nDo you want to try again?(yes/no): ")
  42.  
  43.         if opt.lower() == 'yes':
  44.                 ret=False
  45.         elif opt.lower() == 'no':
  46.                 ret=True
  47.                 print("Exiting program....")
  48.  
  49.         else:
  50.                 print("Please enter yes/no:")
  51.                 break
  52.  
  53.         if ret == False:
  54.                 continue

This script is a simple lap timer that allows the user to measure the time between laps and the total time elapsed. The user can start a new lap by pressing ENTER and stop the timer by pressing CTRL+C. After stopping, the user is asked if they want to restart the timer or exit the program.

Output:

There you have it we successfully created How to Create a Lap Timer in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment