Advertisement
Guest User

Untitled

a guest
Jul 1st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. # Your mission is to create a stopwatch program. this program should have start, stop, and lap options, and it should
  2. # write out to a file to be viewed later.
  3.  
  4. import time
  5. from datetime import timedelta
  6.  
  7.  
  8. def set_time(start_time, round_number):
  9.     stop_time = time.time()
  10.     difference = stop_time - start_time
  11.     displayed_time = str(timedelta(seconds=difference))
  12.  
  13.     with open("records.txt", "a") as records:
  14.         records.write(f"{round_number}\t\t\t{displayed_time}\n")
  15.  
  16.  
  17. def main():
  18.     with open("records.txt","a") as records:
  19.         records.write("Lap\t\t\tTime\n")
  20.  
  21.     round_number = 1
  22.  
  23.     print("Welcome to the Stopwatch program.")
  24.  
  25.     while True:
  26.         command = input("You can [S]tart stopwatch and [S]top or take a [L]ap while stopwatch is running. Command ["
  27.                         "R]records show them. You may leave program by [Q]uiting it.\n")
  28.         if command == "S":
  29.             running = True;
  30.             start_time = time.time()
  31.             while running:
  32.                 command = input("[S] to stop / [L] to take a lap\n")
  33.                 if command == "S":
  34.                     set_time(start_time, round_number)
  35.                     round_number += 1
  36.                     break
  37.                 elif command == "L":
  38.                     start_time = time.time()
  39.                     set_time(start_time, round_number)
  40.                     round_number += 1
  41.  
  42.         elif command == "L":
  43.             print("Sorry, this can be used only while stopwatch is running. [S]tart it.")
  44.  
  45.         elif command == "R":
  46.             with open("records.txt", "r") as records:
  47.                 for line in records:
  48.                     print(line, end="")
  49.         elif command == "Q":
  50.             print("Thanks for using me! ^_^")
  51.             break
  52.         else:
  53.             print("Sorry but I didn't understand your command. Provide it once more.")
  54.  
  55.  
  56. if __name__ == "__main__":
  57.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement