forked from sharunrajeev/YourFirstContribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarm.py
43 lines (37 loc) · 1.29 KB
/
alarm.py
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
from datetime import datetime
def validate_time(alarm_time):
if len(alarm_time) != 11:
return "Invalid time format! Please try again..."
else:
if int(alarm_time[0:2]) > 12:
return "Invalid HOUR format! Please try again..."
elif int(alarm_time[3:5]) > 59:
return "Invalid MINUTE format! Please try again..."
elif int(alarm_time[6:8]) > 59:
return "Invalid SECOND format! Please try again..."
else:
return "ok"
while True:
alarm_time = input("Set a time for the alarm (in HR:MN:SC AM/PM format) ")
validate = validate_time(alarm_time.lower())
if validate != "ok":
print(validate)
else:
print(f"Alarm set to go off in {alarm_time}")
break
alarm_hour = alarm_time[0:2]
alarm_min = alarm_time[3:5]
alarm_sec = alarm_time[6:8]
alarm_period = alarm_time[9:].upper()
while True:
now = datetime.now()
current_hour = now.strftime("%I")
current_min = now.strftime("%M")
current_sec = now.strftime("%S")
current_period = now.strftime("%p")
if alarm_period == current_period:
if alarm_hour == current_hour:
if alarm_min == current_min:
if alarm_sec == current_sec:
print("Alarm Ringing...")
break