Welcome everyone, today we are going to build an Alarm Clock using Python. Project Setup Install required modules/libraries Alright, so first things first! In this project, we are going to use some external modules which are already made available by other developers. These modules will help us save a lot of time and effort. All we have to do is import them into our project to get started. Importing modules is pretty simple. All you have to do is run a simple command from terminal & our specified module will be downloaded in our system. pip install We need 2 different modules for our project - & datetime playsound. Let's run pip install command and download both of these modules. pip install datetime - We will use this module to obtain current time which is not possible without this module. datetime pip install playsound - We will use this module to play our alarm tone once the alarm rings. playsound Download alarm ringtone We are almost there! One last thing before we start our project, we have to download a ringtone which will be played when our alarm goes off. You can download an alarm tone from . Not just alarm tones, you can use any kind of music you like for this project. All you have to do is make sure that the file extension of the audio file is Another thing to make sure is that try to keep the audio file in the same folder as your code. here .wav. Let's Code So the first we are going to do is, of course, import both of our modules, we just installed. datetime datetime playsound playsound from import from import Both of our modules are now ready to use. Now let's ask the user for the time when the alarm will go off. alarm_time = input( ) "Enter time in 'HH:MM:SS AM/PM' format: " We need to have a pre-defined format in which the user will enter the time. Here we are using a standard time format which asks for Hour, minute, second & period (AM/PM). We will save this input into variable. HH:MM:SS AM/PM alarm_time Now we know human errors are very possible and hence we need some way to make sure that the time input the user has provided is exactly in the format we asked for. To do this we will create a function which will do the job of validating the user-provided time, if the time format is unexpected or invalid then our function will display the message on the console and will ask the user to re-enter the time. len(alarm_time) != : : int(alarm_time[ : ]) > : int(alarm_time[ : ]) > : int(alarm_time[ : ]) > : : : def validate_time (alarm_time) if 11 return "Invalid time format! Please try again..." else if 0 2 12 return "Invalid HOUR format! Please try again..." elif 3 5 59 return "Invalid MINUTE format! Please try again..." elif 6 8 59 return "Invalid SECOND format! Please try again..." else return "ok" Here is our function called . Let's break it down and understand what is going on: validate_time - Our function accepts the user input as a parameter . alarm_time - , at we are checking the length of . If not then it will a statement, asking the user to re-enter the value. If the user input is exactly 11 characters long, then block will execute, this is where the more in-depth validation of our user input happens. In first if statement len(alarm_time) != 11 user input to be exactly 11 characters return else - , we are validating the first two characters of our input which are There could be a slight chance that user may enter invalid hour values like something more than 12 hours. Here at we are using a slicing operator to access the first two characters of user input. The input is not more than 12 hours then the execution will move forward to the next conditional statement. But if the input is more than 12 hours, then it will a statement asking the user to re-enter the time. In the first if statement within else block HH. alarm_time[0:2], return - , comparing minutes & seconds respectively. Next two conditional statements do the same job as the first - If the input is all good then, block of our function will return an . Now, this is where the job of our function is over. else OK Awesome! Our function is now ready to use! validate_time Now it's time to call our function. : alarm_time = input( ) validate = validate_time(alarm_time.lower()) validate != : print(validate) : print( ) while True "Enter time in 'HH:MM:SS AM/PM' format: " if "ok" else f"Setting alarm for ..." {alarm_time} break Here we are storing the output of the function into a variable validate which we are using to judge whether the input is valid or not. If it is not valid then the user will be prompted to enter the time again. If not then the execution will head to the next step. Now we are sure that the input provided by the user is valid and now we can separately store the values into different variables. Have a look at the code. alarm_hour = alarm_time[ : ] alarm_min = alarm_time[ : ] alarm_sec = alarm_time[ : ] alarm_period = alarm_time[ :].upper() 0 2 3 5 6 8 9 Here we are using slicing operator to store the specific unit of time into specific variables. will be stored in , in and so on. HH alarm_hour MM alarm_min Coming up next, we now have to get the current time to compare it with the user-provided time. now = datetime.now() current_hour = now.strftime( ) current_min = now.strftime( ) current_sec = now.strftime( ) current_period = now.strftime( ) "%I" "%M" "%S" "%p" Remember our module we imported at the beginning of our project. We are finally gonna make use of it. datetime First, we are using to obtain the current time and we are storing this data in variable. datetime.now() now Next up we are using % notation to extract specific time data from now variable. This is exactly similar to what we just did with user input. is used to the data in string format for comparison. now.strftime() Awesome! We are almost done! alarm_period == current_period: alarm_hour == current_hour: alarm_min == current_min: alarm_sec == current_sec: print( ) playsound( ) if if if if "Wake Up!" 'D:/Library/Documents/Projects/Coding/Beginner Python Projects/Alarm Clock/alarm.wav' Now, this is were the main mechanism of our alarm lies. We are simply using if statements to compare current time & user time. Here, if the user & current period (AM/PM) matches, then the next statement is executed which will make the comparison between user input hour & current hour. This same process repeats until the last statement is executed. if if Finally when the last statement is executed and if it matches, the will be printed on console & the alarm tone will be played. if Wake Up! To play alarm tone we are making use of our module. All we did is passed the of our audio file to the and it played the audio file as per our request. playsound absolute address playsound() Now before we wrap up, it is important for us to put all the code we wrote for the final part, into a loop so that it keeps executing until our alarms rings. : now = datetime.now() current_hour = now.strftime( ) current_min = now.strftime( ) current_sec = now.strftime( ) current_period = now.strftime( ) alarm_period == current_period: alarm_hour == current_hour: alarm_min == current_min: alarm_sec == current_sec: print( ) playsound( ) while True "%I" "%M" "%S" "%p" if if if if "Wake Up!" 'D:/Library/Documents/Projects/Coding/Beginner Python Projects/Alarm Clock/alarm.wav' break Looks good right... We also added a statement at the end so that the execution of our alarm will stop once the alarm has rung. break YOU DID IT! GIVE YOURSELF A PAT ON THE BACK ⭐ Source Code You can find the complete source code of this project here - mindninjaX/Python-Projects-for-Beginners Support Thank you so much for reading! I hope you found this beginner project useful. If you like my work, please consider so that I can bring more projects, more articles for you. Buying me a Coffee Also if you have any questions or doubts feel free to contact me on , & . Or you can also post a comment/discussion & I will try my best to help you :D Twitter LinkedIn GitHub Also published on: https://dev.to/mindninjax/alarm-clock-python-project-4jn4