This code is a simple Python program that sets an alarm based on user input and plays a sound when the alarm time matches the current time.
- Importing Libraries:
import datetime
from playsound import playsound
datetime: This module allows us to work with date and time. It's used here to fetch the current time (hour and minute).
playsound: A Python module used to play audio files. In this case, it plays an alarm sound (alarm.mp3).
- User Input for Alarm Time:
alarmhour = int(input("Enter Hour: "))
alarmmin = int(input("Enter Minute: "))
alarm = input("AM/PM: ")
alarmhour: Prompts the user to enter the hour for the alarm (as an integer).
alarmmin: Prompts the user to enter the minutes for the alarm (as an integer).
alarm: Prompts the user to specify whether the time is in "AM" or "PM".
This is important because the program needs to convert the 12-hour clock format (AM/PM) to the 24-hour clock used by datetime.
- Converting PM to 24-Hour Format:
if alarm == "PM":
alarmhour += 12
If the user specifies "PM", the program adds 12 to the hour input to convert it to the 24-hour format.
For example, if the user inputs 2:30 PM, it is converted to 14:30 to match the 24-hour clock format.
- Infinite Loop to Check Time:
while True:
if alarmhour == datetime.datetime.now().hour and alarmmin == datetime.datetime.now().minute:
print("Playing Alarm Sound...")
playsound("alarm.mp3")
break
Infinite Loop (while True): The program continuously checks the current time and compares it to the user’s alarm time.
When the current time matches the alarm time (both hour and minute), the program prints a message ("Playing Alarm Sound...") and plays the alarm.mp3 sound using the playsound() function.
After playing the sound, the program exits the loop (break).