In the alarm clock script, the user needs to set the time they would like the alarm to go off. The Python script will then display a “Wake up” message to the user when the alarm goes off.

You can also add a countdown to show the user how long they have to wait until the alarm goes off.

How to Set the Alarm

Create a Python Script and add the first printed statements to ask the user when they would like to set the alarm. If you are unfamiliar with some of the Python syntax, you can look at some basic python examples to help you understand the fundamentals.

Create a new file and call it alarm. py. Open the Python script using any text editor such as Notepad++. At the top of the file, import both the datetime and time modules. The program will use these when calculating how long it needs to wait until the alarm goes off. You can use Python’s time module to delay execution, among other actions. import datetimeimport time Add a while loop. Inside the while loop, ask the user to enter a valid alarm time, in the format [hour:minute]. The while loop will repeat if the user enters an invalid number. invalid = True while(invalid):    # Get a valid user input for the alarm time    print(“Set a valid time for the alarm (Ex. 06:30)")    userInput = input("» “) Inside the while loop, convert the user’s input to an array that separates the hour value from the minute value.     # For example, this will convert 6:30 to an array of [6, 30].     alarmTime = [int(n) for n in userInput. split(”:”)] Still inside the while loop, validate both the hour and minute values. The hour should be a number between 0 and 23, and the minute should be a number between 0 and 59. If these conditions are not met, the invalid boolean will cause the while-loop to repeat and ask the user to enter a new value.     # Validate the time entered to be between 0 and 24 (hours) or 0 and 60 (minutes)    if alarmTime[0] >= 24 or alarmTime[0] < 0:        invalid = True    elif alarmTime[1] >= 60 or alarmTime[1] < 0:        invalid = True    else:        invalid = False

How to Count How Long to Wait Until the Alarm Goes Off

Wait until the alarm goes off by counting how many seconds the program will have to wait.

Underneath the while loop, convert the alarm time to the second it is during the day. For reference, there are 86400 seconds in a day. If the user enters 00:01 (one minute after midnight), the alarm time in seconds will be 60. If the user enters 23:59, the alarm time in seconds will be 86340.

Number of seconds in an Hour, Minute, and Secondseconds_hms = [3600, 60, 1]# Convert the alarm time to secondsalarmSeconds = sum([a*b for a,b in zip(seconds_hms[:len(alarmTime)], alarmTime)]) Use the datetime.

now() function to determine the current time. Convert the current time of day in seconds. now = datetime. datetime. now()currentTimeInSeconds = sum([a*b for a,b in zip(seconds_hms, [now. hour, now. minute, now. second])]) Calculate the number of seconds until the alarm goes off. secondsUntilAlarm = alarmSeconds - currentTimeInSeconds If the time different is negative, that means that the alarm needs to be set for the next day. if secondsUntilAlarm < 0:    secondsUntilAlarm += 86400 # number of seconds in a day Display a message to the user to let them know that the alarm has been successfully set. print(“Alarm is set!")print(“The alarm will ring at %s” % datetime. timedelta(seconds=secondsUntilAlarm))

How to Ring the Alarm

To ring the alarm, wait for the remaining seconds before printing a “Wake up!” message to the user.

Use time. sleep to wait for the amount of seconds required until the alarm needs to go off. time. sleep(secondsUntilAlarm) Display the “Wake up” message to the user when the alarm goes off. print(“Ring Ring. . . time to wake up!”)

How to Add a Countdown Before the Alarm Rings

To add a countdown for each second, use a for loop to print the seconds left to the user.

Replace the time. sleep line. Add a for loop for each second until the alarm goes off, and display the seconds left to the user. for i in range(0, secondsUntilAlarm):    time. sleep(1)    secondsUntilAlarm -= 1    print(datetime. timedelta(seconds=secondsUntilAlarm))

How to Run the Alarm Clock Program

Run the script by navigating to the file using the command line. Use the python command to start the script, and set an alarm time.

Open a command prompt or terminal. Navigate to the location of your alarm. py file. For example, if you have stored your Python file on the Desktop, use cd Desktop. cd Desktop Use the python command to run the Python script. python alarm. py Set a valid time for the alarm, in the format of [hour:minute]. For example, 4:30. The alarm will set and start a countdown for each second until the alarm needs to go off. Wait for the alarm to finish counting down. Once the program has completed the countdown, the program will display a message to show you that the alarm is ringing.

Create Simple Programs Using Python

An alarm clock is a good exercise that you can practice to sharpen your Python skills. You can write the alarm clock app in a single script file, and run it using a command line. At runtime the program will ask the user to enter a valid time to set the alarm.

Once the alarm is set, calculate how many seconds you will need to wait until the alarm clock goes off. Make the program wait for that long before displaying a message to the user when the alarm goes off.

You can also re-create this alarm exercise in other languages. You can learn how to create a digital clock using HTML, CSS, and JavaScript.