来用python自己做一个闹钟吧

闹钟

是一种具有可以在预先设定的时间被激活以响铃的功能的时钟,用于唤醒打工人们。

使用Python中的DateTime模块来创建闹钟,并用Python中的playsound库来播放闹钟声音。

###想要学习Python?Python学习交流群:660193417 满足你的需求,资料都已经上传群文件,可以自行下载!###
from datetime import datetime
from playsound import playsound

# 输入
alarm_time = input("请输入闹钟时间, 示例: 09:50:00 am\n")
# 时
alarm_hour = alarm_time[0:2]
# 分
alarm_minute = alarm_time[3:5]
# 秒
alarm_seconds = alarm_time[6:8]
# 上午或下午
alarm_period = alarm_time[9:11].upper()
print("完成闹钟设置..")

while True:
    now = datetime.now()
    current_hour = now.strftime("%I")
    current_minute = now.strftime("%M")
    current_seconds = now.strftime("%S")
    current_period = now.strftime("%p")

    # 时间判断
    if alarm_period == current_period:
        if alarm_hour == current_hour:
            if alarm_minute == current_minute:
                if alarm_seconds == current_seconds:
                    print("起来啦!")
                    # 闹钟铃声
                    playsound('audio.mp3')
                    break

来测试一下,设置一个闹钟,到指定时间就会有音乐响起。
请添加图片描述

是不是很简单~ 来~试试看吧!

猜你喜欢

转载自blog.csdn.net/m0_67575344/article/details/125149577