-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown_timer.py
More file actions
58 lines (48 loc) · 1.58 KB
/
countdown_timer.py
File metadata and controls
58 lines (48 loc) · 1.58 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import sys
import time
error_msg = '\n[!] Error: Invalid input.\n'
error_msg2 = '\n[!] Error: Must be a positive integer.\n'
def countdown_timer(hours, minutes, seconds):
return hours*3600 + minutes*60 + seconds
def get_positive_input(prompt):
while True:
try:
value = int(input(prompt))
if value < 0:
print(error_msg2)
continue
return value
except ValueError:
print(error_msg)
def user_input():
hours = get_positive_input("Enter hours: ")
minutes = get_positive_input("Enter minutes: ")
seconds = get_positive_input("Enter seconds: ")
print()
count_time = countdown_timer(hours, minutes, seconds)
for i in range(count_time, 0, -1):
hours, rem = divmod(i,3600)
minutes, seconds = divmod(rem, 60)
print("Time remaining: {:02d}:{:02d}:{:02d}".format(hours,minutes,seconds),end='\r')
time.sleep(1)
print("\nTime's up!")
def main():
os.system('cls' if os.name == 'nt' else 'clear')
app_name = 'Countdown Timer'
print(f'{"-" * 48}')
print(f'{" " * 12}{app_name}{" " * 12}')
print(f'{"-" * 48}')
user_input()
while True:
response = input('\nDo you want to continue? (Y/N) ')
if response == 'y' or response == 'Y':
main()
elif response == 'n' or response == 'N':
print('\nThank you and have a great day.\n')
sys.exit()
else:
print('\nError: Please select Y or N.\n')
continue
if __name__ == '__main__':
main()