The task is to create a stopwatch.
In the below program, the stopwatch will be started when you press the ENTER key and stopped when you press the CTRL+C key.
Logic: To run the stopwatch (count the time), we are writing the code in an infinite loop, start time will be saved in start_time variable as you press the ENTER and when you press the CTRL + C a KeyboardInterrupt exception will generate and we will again get the time, which will be considered as end_time. Now, to calculate the difference – we will simply subtract the time from end_time to start_time.
To get the time in seconds, we are using time() function of the time module. So, you need to import the time module first.
Python code for a stopwatch
Output
need an explanation for this answer? contact us directly to get an explanation for this answer