mengyuxin

stop_watch.py

Jan 6th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. #! python3
  2. # stop_watch.py - A simple stopwatch program.
  3.  
  4. import time
  5.  
  6. # Display the program's instructions.
  7. print('Press ENTER to begin. \nAfterwards, press ENTER to "click" the stopwatch.\nPress Ctrl+C to quite.')
  8. input() # press ENTER to begin
  9. print('Started.')
  10. start_time = time.time()    # get the first lap's start time
  11. last_time = start_time
  12. lap_num = 1
  13.  
  14. # Start tracking the lap times.
  15. try:
  16.     while True:
  17.         input()
  18.         lap_time = round(time.time() - last_time, 2)
  19.         total_time = round(time.time() - start_time, 2)
  20.         print('Lap #%s: %s (%s)' % (lap_num, total_time, lap_time), end = '')
  21.         lap_num += 1
  22.         last_time = time.time() # reset the last lap time
  23. except KeyboardInterrupt:
  24.     # Handle the Ctrl+C exception to keep its error message from displaying.
  25.     print('\nDone!')
Add Comment
Please, Sign In to add comment