-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch.py
More file actions
40 lines (34 loc) · 1.1 KB
/
Copy pathstopwatch.py
File metadata and controls
40 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import time
def stopwatch():
print("⏱️ Simple Stopwatch")
print("Press [Enter] to start...")
input()
start_time = time.time()
print("Stopwatch started. Press [Enter] to stop.")
input()
end_time = time.time()
elapsed = end_time -start_time
minutes = int(elapsed // 60)
seconds = int(elapsed % 60)
milliseconds = int((elapsed - int(elapsed)) * 1000)
print(f"\n⏱ Elapsed time: {minutes}m {seconds}s {milliseconds}ms")
def countdown(seconds):
print(f"\n⏳ Countdown starting for {seconds} seconds...")
while seconds:
mins, secs = divmod(seconds, 60)
print(f"{mins:02}:{secs:02}", end='\r')
time.sleep(1)
seconds -= 1
print("⏰ Time's up!")
if __name__ == "__main__":
choice = input("Start stopwatch or countdown? (s/c): ").lower()
if choice == 's':
stopwatch()
elif choice == 'c':
try:
secs = int(input("Enter countdown time in seconds: "))
countdown(secs)
except ValueError:
print("❌ Invalid input.")
else:
print("Invalid choice.")