Easy Tutorial
❮ Python Bank System Python Att List Index ❯

Python Implementation of a Stopwatch Function

The following example uses the time module to implement a stopwatch function:

Example

import time

print('Press Enter to start the timer, and Ctrl + C to stop it.')
while True:

    input("") # Use raw_input() for Python 2.x versions
    starttime = time.time()
    print('Started')
    try:
        while True:
            print('Timing: ', round(time.time() - starttime, 0), 'seconds', end="\r")
            time.sleep(1)
    except KeyboardInterrupt:
        print('Stopped')
        endtime = time.time()
        print('Total time: ', round(endtime - starttime, 2), 'secs')
        break

Test results:

Press Enter to start the timer, and Ctrl + C to stop it.

Started
Timing:  3.0 seconds
Timing:  5.0 seconds
^CStopped 6.0 seconds
Total time: 6.69 secs
❮ Python Bank System Python Att List Index ❯