A lightweight, efficient alarm clock implementation in Python (under 100 lines) that supports multiple alarms with custom or random wake-up sounds. Perfect for daily use and learning Python's time management and sound handling capabilities.
- ⏰ Multiple alarm support
- 🎵 Custom sound selection
- 🎲 Random sound option
- ⌚ 24-hour time format
- 💻 Lightweight (< 100 lines)
- 📊 Sound file verification
- 🔄 Automatic test alarm
- 🛑 Clean exit with Ctrl+C
- Python 3.6+
- pygame library
# 1. Install pygame
pip install pygame
# 2. Create project structure
mkdir alarm_clock
cd alarm_clock
mkdir sounds
# 3. Add sound files to the sounds directory
# (beep.mp3, birds.mp3, rooster.mp3)
# 4. Run the alarm clock
python alarm_clock.pyalarm_clock/
│
├── alarm_clock.py
├── README.md
└── sounds/
├── beep.mp3
├── birds.mp3
└── rooster.mp3
python alarm_clock.pyThis will automatically set a test alarm for 1 minute from current time.
from alarm_clock import AlarmClock
clock = AlarmClock()
clock.add_alarm("07:00", "birds") # Specific sound
clock.add_alarm("07:30") # Random sound
clock.run()- Use 24-hour format (HH:MM)
- Examples:
- "07:00" = 7 AM
- "13:30" = 1:30 PM
- "00:00" = Midnight
Place MP3 files in the sounds directory with these names:
- beep.mp3
- birds.mp3
- rooster.mp3
The program will verify sound files on startup and show their status:
Sound files status:
✅ beep: /path/to/beep.mp3
✅ birds: /path/to/birds.mp3
❌ rooster: /path/to/rooster.mp3 # Missing file
- Checks for sound files on startup
- Shows ✅ for found files
- Shows ❌ for missing files
- Fallback to system beep if sound file missing
- Clear error messages for troubleshooting
- Graceful exit with Ctrl+C
- Checks alarms every 30 seconds
- Low CPU usage
- Automatic cleanup of triggered alarms
- Check sound file status in startup message
- Verify pygame installation:
pip install --upgrade pygame - Check system volume
- Try different sound file formats
- "Invalid time format": Use HH:MM format (e.g., "09:30")
- "Sound file not found": Add MP3 files to sounds directory
- "Error playing sound": Check file format and pygame installation
- The program automatically sets a test alarm 1 minute ahead
- Use Ctrl+C to cleanly exit the program
- Missing sound files trigger system beep as fallback
- Random sound selection if no specific tone specified