
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Create a Lap Timer
When it is required to create a lap timer using Python, the ‘time’ method is used. The number of laps is predefined, and a try catch block is defined, to start the lap timer.
Below is a demonstration of the same −
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 To tal Time: 1.77 Lap Time: 1.77 ******************** Lap No. 2 Total Time: 3.52 Lap Time: 1.75 ******************** Exit!
Explanation
The required packages are imported.
The start time, end time and number of laps are defined.
The timer starts by clicking on ‘Enter’.
In the try catch block, the difference between current time and end time is determined.
Again, the difference between current time and start time is determined.
This gives the number of laps, total time, and lap time.
This is displayed as output on the console.
In the ‘except’ block, the ‘Exit’ is defined.
- Related Articles
- How to create a timer using tkinter?
- How to create a countdown timer with JavaScript?
- How to create timer using C++11?
- How to create a high resolution timer with C++ and Linux?
- Timer objects in Python
- C# Program to set the timer to zero
- Python Program to create a String object
- Making a countdown timer with Python and Tkinter
- How to get timer ticks at a given time in Python?
- Python program to create a dictionary from a string
- Python program to create 3D list.
- Python Program to Create a class performing Calculator Operations
- Python Program to create a Tuple using tuple literal
- Python Program To Create A Dictionary With A Dictionary Literal
- Program to create grade calculator in Python

Advertisements