0
0
PythonProgramBeginner · 2 min read

Python Program to Create Alarm Clock

You can create an alarm clock in Python by using datetime to check the current time in a loop and playsound to play an alarm sound when the set time matches, for example: while True: if datetime.now().strftime('%H:%M') == alarm_time: playsound('alarm.mp3').
📋

Examples

InputSet alarm_time = '07:30'
OutputAt 07:30, the program plays the alarm sound and prints 'Wake up! Alarm ringing...'
InputSet alarm_time = '23:59'
OutputAt 23:59, the program plays the alarm sound and prints 'Wake up! Alarm ringing...'
InputSet alarm_time = '00:00'
OutputAt 00:00, the program plays the alarm sound and prints 'Wake up! Alarm ringing...'
🧠

How to Think About It

To create an alarm clock, first get the alarm time from the user. Then, keep checking the current time repeatedly using a loop. When the current time matches the alarm time, play a sound and notify the user. Use simple time comparison and a sound playing library to achieve this.
📐

Algorithm

1
Get the alarm time input from the user in HH:MM format
2
Start an infinite loop to keep checking the current time
3
Get the current time in HH:MM format
4
Compare current time with alarm time
5
If they match, play the alarm sound and print a message
6
Break the loop to stop the alarm
💻

Code

python
from datetime import datetime
from playsound import playsound

alarm_time = input('Set the alarm time (HH:MM): ')
print(f'Alarm set for {alarm_time}')

while True:
    now = datetime.now().strftime('%H:%M')
    if now == alarm_time:
        print('Wake up! Alarm ringing...')
        playsound('alarm.mp3')
        break
Output
Set the alarm time (HH:MM): 07:30 Alarm set for 07:30 Wake up! Alarm ringing...
🔍

Dry Run

Let's trace setting alarm_time to '07:30' and current time reaching '07:30'.

1

Input alarm time

User inputs '07:30', so alarm_time = '07:30'

2

Start loop and get current time

Current time is checked repeatedly, e.g., '07:29', '07:30'

3

Compare times

When now == alarm_time ('07:30' == '07:30'), condition is True

4

Trigger alarm

Print message and play sound, then break loop

IterationCurrent TimeAlarm TimeMatch?
107:2907:30No
207:3007:30Yes
💡

Why This Works

Step 1: Getting alarm time

We ask the user to enter the alarm time as a string in HH:MM format to know when to trigger the alarm.

Step 2: Checking current time

We use datetime.now().strftime('%H:%M') to get the current time in the same format for easy comparison.

Step 3: Playing alarm

When the current time matches the alarm time, we use playsound to play an alarm sound and notify the user.

🔄

Alternative Approaches

Using time.sleep to reduce CPU usage
python
from datetime import datetime
from playsound import playsound
import time

alarm_time = input('Set the alarm time (HH:MM): ')
print(f'Alarm set for {alarm_time}')

while True:
    now = datetime.now().strftime('%H:%M')
    if now == alarm_time:
        print('Wake up! Alarm ringing...')
        playsound('alarm.mp3')
        break
    time.sleep(20)
This method adds a 20-second pause in each loop to reduce CPU usage but may delay alarm accuracy slightly.
Using threading to allow alarm and other tasks
python
import threading
from datetime import datetime
from playsound import playsound

def alarm(alarm_time):
    while True:
        now = datetime.now().strftime('%H:%M')
        if now == alarm_time:
            print('Wake up! Alarm ringing...')
            playsound('alarm.mp3')
            break

alarm_time = input('Set the alarm time (HH:MM): ')
thread = threading.Thread(target=alarm, args=(alarm_time,))
thread.start()
print('Alarm is running in background')
This method runs the alarm in a separate thread so the program can do other things simultaneously.

Complexity: O(T) time, O(1) space

Time Complexity

The program runs a loop checking the time every iteration until the alarm time is reached, so time depends on how long until the alarm.

Space Complexity

Uses constant space for storing strings and no extra data structures.

Which Approach is Fastest?

Adding time.sleep() reduces CPU load without affecting alarm accuracy much, while threading allows multitasking but adds complexity.

ApproachTimeSpaceBest For
Simple loopO(T)O(1)Basic alarm functionality
Loop with sleepO(T)O(1)CPU efficient waiting
ThreadingO(T)O(1)Running alarm alongside other tasks
💡
Use time.sleep() inside the loop to avoid high CPU usage while waiting for the alarm time.
⚠️
Beginners often forget to format the current time and alarm time the same way, causing the comparison to fail.