From 5bfe41605d13ca04b2197e4b12f3eca701fbe677 Mon Sep 17 00:00:00 2001 From: LunaAsefaw <90245715+LunaAsefaw@users.noreply.github.com> Date: Wed, 19 Oct 2022 00:42:38 +0200 Subject: [PATCH] Update alarm.clock When I first ran this, I used 24 hours format and it did not give me errors or any messages but kept running for a while until I interrupted the code. Therefore, I have added 'am/pm' into the input text and created if statements to reject 24 format answers. Secondly, I have added a snooze function. If the user says yes to snoozing, the alarm goes off again five seconds later --- Python/Alarm_Clock.py | 51 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/Python/Alarm_Clock.py b/Python/Alarm_Clock.py index cf1d895..72ff69b 100644 --- a/Python/Alarm_Clock.py +++ b/Python/Alarm_Clock.py @@ -1,21 +1,52 @@ from datetime import datetime from playsound import playsound -alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n") +import time + +def validate_time(alarm_time): + if len(alarm_time) != 11: + return "Invalid time format! Please include space after seconds and add AM/PM" + 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("Enter the time of alarm to be set:HH:MM:SS in AM/PM format\n") + + validate=validate_time(alarm_time.lower()) + if validate!="ok": + print(validate) + else: + print("Setting up alarm..") + break alarm_hour=alarm_time[0:2] alarm_minute=alarm_time[3:5] alarm_seconds=alarm_time[6:8] -alarm_period = alarm_time[9:11].upper() -print("Setting up alarm..") +alarm_period = alarm_time[9:11].upper() + while True: now = datetime.now() current_hour = now.strftime("%I") current_minute = now.strftime("%M") current_seconds = now.strftime("%S") current_period = now.strftime("%p") - if(alarm_period==current_period): - if(alarm_hour==current_hour): - if(alarm_minute==current_minute): - if(alarm_seconds==current_seconds): - print("Wake Up!") - playsound('audio.mp3') - break + + if(alarm_hour==current_hour): + if(alarm_minute==current_minute): + if(alarm_seconds==current_seconds): + + print("Wake Up!") + playsound('audio.mp3') + snooze= input("Would you like to snooze? Yes/No response\n") + if snooze=='Yes': + time.sleep(5) + print("Wake up") + playsound('audio.mp3') + break + else: + break