CodeSolved

A comprehensive source of programming questions and exercises

The alarm

Practice Easy 1209/ Download 389 Views

Write a program that receives hours and minutes from the user and the alarm at the specified time

2 Answers

import time
import datetime

def set_alarm(alarm_time):
    print(f"زنگ هشدار برای ساعت {alarm_time} تنظیم شد.")
    while True:
        # Receive current time
        current_time = datetime.datetime.now().strftime("%H:%M")
        if current_time == alarm_time:
            print("زنگ هشدار! زمان آن رسیده است!")
            # Play the alarm
            # In Windows you can use WinSound
            # import winsound
            # winsound.Beep(1000, 1000)  # 1000 Hz frequency for 1000 mm
            # You can use PlaySound for other operating systems
            from playsound import playsound
            playsound('alarm_sound.mp3')  # Alarm file name
            break
        time.sleep(30)  # Check every 30 seconds

# Receive hours and minutes from the user
hour = input("لطفاً ساعت را وارد کنید (HH): ")
minute = input("لطفاً دقیقه را وارد کنید (MM): ")

# Combination of hours and minutes to format HH: mm
alarm_time = f"{hour}:{minute}"

# Set the alarm alarm
set_alarm(alarm_time)
Mma123 Download Python
import time
import winsound  # For Windows
# Use PlaySound if you are on Linux or Mac.

def alarm_clock(hour, minute):
    # Convert hours and minutes to current time
    while True:
        current_time = time.localtime()
        current_hour = current_time.tm_hour
        current_minute = current_time.tm_min
        
        # Check time
        if current_hour == hour and current_minute == minute:
            print("زنگ هشدار! زمان مشخص شده فرا رسیده است.")
            # Play the alarm
            winsound.Beep(1000, 1000)  # Alarm (1000 Hz for 1 second)
            break
        
        # Sleeping for 30 seconds before checking again
        time.sleep(30)

if __name__ == "__main__":
    # Receive hours and minutes from the user
    hour = int(input("لطفاً ساعت (0-23) را وارد کنید: "))
    minute = int(input("لطفاً دقیقه (0-59) را وارد کنید: "))
    
    # Alarm
    alarm_clock(hour, minute)
User 136 Download Python

Submit answer

Submitting answers is currently unavailable.

×
×
Close