Python Alarm Clock Project

Python project 07

Python Alarm Clock Project

Last updated: 31 August 2026

Build a command-line alarm clock that accepts a time and message, then checks the current time until the alarm is due. It is a clear introduction to dates, times, loops, and graceful cancellation.

Build time-based Python programsView all project ideas

What you will build

The program validates a 24-hour time such as 07:30, saves it for the current session, and prints an alarm message when that time arrives.

Skills practised

  • datetime and time modules
  • Input validation
  • Loops and conditions
  • KeyboardInterrupt handling

Requirements

  • Python 3.8 or later
  • A terminal or command prompt
  • No external packages
  • About 30 minutes to build

Full Python code

Save it as alarm_clock.py, then run it from a terminal.

"""A simple command-line alarm clock."""

import time
from datetime import datetime


def read_alarm_time():
    while True:
        value = input("Alarm time (HH:MM, 24-hour format): ").strip()
        try:
            datetime.strptime(value, "%H:%M")
            return value
        except ValueError:
            print("Use a valid time such as 07:30 or 18:45.")


def main():
    print("Python Alarm Clock")
    alarm_time = read_alarm_time()
    message = input("Alarm message: ").strip() or "Time is up!"
    print(f"Alarm set for {alarm_time}. Press Ctrl+C to cancel.")

    try:
        while True:
            now = datetime.now().strftime("%H:%M")
            if now == alarm_time:
                print("\n" + "* " * 12)
                print(f"ALARM: {message}")
                print("* " * 12)
                break
            time.sleep(15)
    except KeyboardInterrupt:
        print("\nAlarm cancelled.")


if __name__ == "__main__":
    main()

How it works

read_alarm_time() uses datetime.strptime() to validate the format. The main loop compares the selected time with the current local time, sleeping briefly between checks so it does not use unnecessary CPU.

Run the project

  1. Run python alarm_clock.py.
  2. Enter a time in 24-hour format.
  3. Enter a short message.
  4. Use Ctrl+C if you need to cancel the waiting alarm.

Notes for your notebook

  • 24-hour time: 18:45 represents 6:45 PM.
  • Sleep: pauses a program between checks.
  • Production alarms: need notifications or sound support suited to the operating system.

Ways to extend it

Add a countdown, multiple alarms, a sound file, or a Tkinter clock interface.

What to learn next

Strengthen your foundations through Python functions and choose another task from the Softenant project library.