Alarm Clock in Python
About the project:This Python script will allow you to set an alarm for a specific time, and when that time arrives, it will display a message.
For simplicity, in a basic console application, it will print a message repeatedly rather than playing an audible sound, as sound playback often requires additional libraries that might need to be installed.
Allows the user to set an alarm time and a custom message. The alarm will trigger when the current time matches the set time.
The else part of the loop is optional to show current time. You can uncomment it if you want to show current time
How to use this alarm clock:
- Run the Python script.
- It will prompt you to enter the hour (0-23) and minute (0-59) for your alarm.
- Then, you can enter a custom message that will be displayed when the alarm goes off.
- The script will then wait. When the current time matches your set alarm time, it will repeatedly print your alarm message to the console for a few seconds.
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: choose any code snippet and follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# alarm_clock.py
import datetime
import time
import os
def set_alarm():
"""
Allows the user to set an alarm time and a custom message.
The alarm will trigger when the current time matches the set time.
"""
print("--- Python Alarm Clock ---")
print("Set your alarm time.")
# Get the alarm hour from the user
while True:
try:
alarm_hour = int(input("Enter hour (0-23): ").strip())
if 0 <= alarm_hour <= 23:
break
else:
print("Invalid hour. Please enter a number between 0 and 23.")
except ValueError:
print("Invalid input. Please enter a number.")
# Get the alarm minute from the user
while True:
try:
alarm_minute = int(input("Enter minute (0-59): ").strip())
if 0 <= alarm_minute <= 59:
break
else:
print("Invalid minute. Please enter a number between 0 and 59.")
except ValueError:
print("Invalid input. Please enter a number.")
# Get the alarm message from the user
alarm_message = input("Enter a message for your alarm (e.g., 'Time to wake up!'): ").strip()
if not alarm_message:
alarm_message = "Alarm!" # Default message if user provides none
print(f"\nAlarm set for {alarm_hour:02d}:{alarm_minute:02d} with message: '{alarm_message}'")
print("Waiting for alarm to trigger...")
# Loop indefinitely until the alarm time is reached
while True:
# Get the current time
now = datetime.datetime.now()
current_hour = now.hour
current_minute = now.minute
current_second = now.second
# Check if the current time matches the alarm time
if current_hour == alarm_hour and current_minute == alarm_minute:
print("\n" + "="*30)
print(f"!!! ALARM !!! {alarm_message}")
print("="*30 + "\n")
# Ring for a few seconds (print message repeatedly)
for _ in range(5): # Print 5 times
print(f"!!! ALARM !!! {alarm_message}")
time.sleep(1) # Wait for 1 second between messages
print("\nAlarm finished. You can close the program or set a new alarm.")
break # Exit the loop once the alarm has gone off
else:
# Display current time (optional, for user feedback)
# print(f"Current time: {current_hour:02d}:{current_minute:02d}:{current_second:02d}")
# Wait for 1 second before checking again to avoid busy-waiting
time.sleep(1)
# This ensures that set_alarm() is called only when the script is executed directly.
if __name__ == "__main__":
set_alarm()
Important Notes:
- This is a console-based alarm. It will not play an audible sound without installing additional Python libraries (like playsound or pygame). If you want sound, you would need to install such a library and modify the set_alarm function to use it.
- The script will block your console while it's waiting for the alarm. You won't be able to type other commands in that console window until the alarm triggers or you manually stop the script (e.g., by pressing Ctrl+C).
- The alarm checks the time every second.