Python 3我无法发出警报

时间:2018-04-24 01:52:16

标签: python python-3.x

所以我有一个代码,我正在努力在我指定的时间自动播放闹钟。它还播放一些城市声音来帮助我睡觉,在播放这些声音时,我无法通过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)

1 个答案:

答案 0 :(得分:0)

由于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)