Stopwatch Program in Python

Stopwatch program in python. a watch that can be started and stopped by pressing a button, allowing you to time something precisely. A stopwatch is a timepiece that can be worn on one’s wrist and is used to measure the time between activation and deactivation. A stop clock is a large digital version of a stopwatch designed for viewing from a distance, such as in a sports stadium.

import time
def time_convert(sec):
mins = sec //60
sec = sec % 60
hours = mins // 60
mins = mins % 60
print("Time Lapsed = {0}:{1}:{2}".format(int(hours),int(mins),sec))
input("Press Enter to start stopwatch")
start_time = time.time()
input("Press Enter to stop stopwatch")
end_time = time.time()
Time_taken = end_time - start_time
time_convert(Time_taken)

 

Output:

Press Enter to start the stopwatch
Press Enter to stop the stopwatch
Time Lapsed = 0:0:2.5889599323272705

Import in Python

The import statement is the most common method of triggering the import machinery, but it is by no means the only one. When the import is used, the __import__() function searches for the module in the local scope first. The function’s return value is then reflected in the initial code’s output. The import keyword is followed by the module’s name in an import statement. This will be declared at the top of the Python file, before any shebang lines or general comments.

Time Function in Python

The time module’s time.time() function will be used. The documentation can be found here. The time. time() function keeps track of how many seconds have passed since the day began. We’ll need to import the time module into our code before we can use this function.

Procedures to Execute the Time Function in Python

We’ll bring in the time module.
To start the stopwatch, the user will press Enter.
At this point, time. time is used to set start time ().
At this point, time. time is used to set start time ().
As a result, start time now contains the number of seconds.
The clock will now be running in the background.
The user will now press Enter once more to stop the stopwatch. 
end time is set using time. time at this point (). 
end time is set using time. time at this point (). 
So, when the clock is stopped, the end time has the number of seconds that have passed.