Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Your mission is to create a stopwatch program. this program should have start, stop, and lap options, and it should
- # write out to a file to be viewed later.
- import time
- from datetime import timedelta
- def set_time(start_time, round_number):
- stop_time = time.time()
- difference = stop_time - start_time
- displayed_time = str(timedelta(seconds=difference))
- with open("records.txt", "a") as records:
- records.write(f"{round_number}\t\t\t{displayed_time}\n")
- def main():
- with open("records.txt","a") as records:
- records.write("Lap\t\t\tTime\n")
- round_number = 1
- print("Welcome to the Stopwatch program.")
- while True:
- command = input("You can [S]tart stopwatch and [S]top or take a [L]ap while stopwatch is running. Command ["
- "R]records show them. You may leave program by [Q]uiting it.\n")
- if command == "S":
- running = True;
- start_time = time.time()
- while running:
- command = input("[S] to stop / [L] to take a lap\n")
- if command == "S":
- set_time(start_time, round_number)
- round_number += 1
- break
- elif command == "L":
- start_time = time.time()
- set_time(start_time, round_number)
- round_number += 1
- elif command == "L":
- print("Sorry, this can be used only while stopwatch is running. [S]tart it.")
- elif command == "R":
- with open("records.txt", "r") as records:
- for line in records:
- print(line, end="")
- elif command == "Q":
- print("Thanks for using me! ^_^")
- break
- else:
- print("Sorry but I didn't understand your command. Provide it once more.")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement