shrey141102 / PythonProjects

Python Programs
8 stars 37 forks source link

Alarm Clock #6

Closed shrey141102 closed 7 months ago

shrey141102 commented 7 months ago

Create a simple alarm clock that allows users to set a specific time for an alarm to go off.

Tejas1674 commented 7 months ago

import datetime import time

def set_alarm(): print("Enter the time for the alarm (24-hour format HH:MM):") while True: alarm_time_str = input(">> ") try: alarm_time = datetime.datetime.strptime(alarm_time_str, "%H:%M") break except ValueError: print("Invalid time format. Please use HH:MM format.")

return alarm_time

def main(): print("Simple Alarm Clock")

# Set the alarm
alarm_time = set_alarm()
print(f"Alarm set for {alarm_time.strftime('%H:%M')}")

while True:
    current_time = datetime.datetime.now().strftime("%H:%M")
    if current_time == alarm_time.strftime("%H:%M"):
        print("ALARM! ALARM! ALARM!")
        break

    # Check every 1 minute
    time.sleep(60)

if name == "main": main()

shrey141102 commented 7 months ago

Raise a pr