- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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!")
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!
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.
- How to create a timer using tkinter?
- How to create timer using C++11?
- Timer objects in Python
- How to create a high resolution timer with C++ and Linux?
- C# Program to set the timer to zero
- 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
- How to make a countdown timer in Android?
- Timer in C#
- Python program to create 3D list.
- Python Program to Create a class performing Calculator Operations
- How to set a timer in Android using Kotlin?