Skip to content

Sayanprogramming/Make-Alarm-Clock-using-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

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.

  1. 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).

  1. 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.

  1. 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.

  1. 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).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages