Python3我的闹钟响不起来了

2024-10-03 00:21:42 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个代码,我正在工作的自动播放我的闹钟在我指定的时间。它还播放一些城市的声音来帮助我入睡,而当播放这些声音的时候,我不能让while循环结束来播放警报声。你知道吗

我试图将while循环引入到我创建的报警函数中,但没有起作用,但我相当肯定它与我的while循环有关。但我想换一双新眼睛可能会对这段代码有好处。你知道吗

'''
Title: This is an alarm that perfectly lines up with my sleep schedule
Author: Riley Carpenter
'''
from pygame import mixer
import time
import os
import sys
import random
snoozeorstop = " "
Currenttime = time.ctime()
Hours1 = int(Currenttime[11:13])
Minutes1 = int(Currenttime[14:16])
Seconds = int(Currenttime[17:19])
optionalsongs = ["Pink Floyd     Time.wav","Alarm2.wav","Alarm3.wav","Alarm4.wav","Alarm5.wav","Alarm6.wav","Alarm7.wav","Alarm8.wav","Alarm9.wav","Alarm10.wav","Alarm11.wav"]
phrases = ["Wake up Riley!!!!","It's time to wake up it's time to wake up","HEEEEYYY WAKE UP","RILEY RILEY RILEY WAKE UP","1 2 3 4 5 6 7 8 9 it is time to wake up","Riley more alarms are to come UNLESS you get up","OH WHEN SHALL I SEE JESUS you wanna not hear this again? Wake up","I'm so tired of telling you to wake up just wake up","A friend of the devil is somehow who doesn't wake up","Babe babe bae wake up"]
def playsound(soundfile):
    mixer.init()
    mixer.music.load(soundfile)
    mixer.music.play(-1)
def stopsound():
    mixer.music.stop()
def alarm(hour,minute):
    print("")
    print("")
    print(random.choice(phrases))
    if Hours1 == hour and Minutes1 == minute:
        stopsound()
        playsound(random.choice(optionalsongs))
        print("")
        snoozeorstop = input("Do you want to stop the song? ")
        if snoozeorstop == "stop":
            stopsound()
citysounds = input("Do you want to play the soothing sounds of the city? ")
if citysounds == "y" or citysounds == "Yes" or citysounds == "Y" or     citysounds == "yes":
    playsound("Citys Night ambience sounds.wav")
else:
    playsound("Beginningsound.wav")
amount = 0
print("This is how many second has passed since this alarm was turned on")
while Hours1 != 5 and Minutes1 != 0:
    Currenttime = time.ctime()
    print(amount)
    Hours1 = int(Currenttime[11:13])
    Minutes1 = int(Currenttime[14:16])
    Seconds = int(Currenttime[17:19])
    amount += 1
    time.sleep(1)
alarm(5,00)
alarm(5,5)
alarm(5,10)
alarm(5,15)
alarm(5,20)
alarm(5,25)
alarm(5,30)
alarm(5,35)
alarm(5,40)
alarm(5,45)
alarm(5,50)
alarm(5,55)
alarm(6,00)
alarm(6,5)
alarm(6,10)
alarm(6,15)
alarm(6,20)
alarm(6,25)

Tags: toimportyoutimeisintprintup
1条回答
网友
1楼 · 发布于 2024-10-03 00:21:42

既然while循环是程序的核心,为什么不先编写这段代码呢。当这项工作的预期,你可以添加歌曲列表等

至于检查闹钟何时应该响,你可能想检查一下datetime模块,它使时间计算变得更容易。例如:

import datetime, time

start = datetime.datetime.now()
alarm = start + datetime.timedelta(seconds=5)   # Set alarm time 5 seconds from now

while True:
    now = datetime.datetime.now()    # When is this?
    print(now.strftime("%H:%M:%S"))  # Print human readable time
    if now > alarm:       # Have I passed the alarm time?
        print('Alarm')    # Sound the alarm
        break             # Leave this loop
    time.sleep(1)

相关问题 更多 >