福图特定时间的投票

2024-10-04 09:28:09 发布

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

所以我一直在用python2.7在raspberry pi上编程一个会说话的人工智能程序,我遇到了一个错误!你知道吗

我试着设置一个闹钟功能,这样你就可以设置你想要它叫醒你的时间,然后它在这个时间播放一首歌,然后在读出一个设置好的信息之后。我已经得到了相当远的代码,它工作得非常好,但早些时候突然停止工作,我不知道为什么。感觉好像跳过了while循环,但我真的不确定!你知道吗

这是代码,忽略“speak”命令,它只是让它说话。你知道吗

if userInput == "set alarm":
            alarmnow = datetime.datetime.now()
            espeak.synth("What hour would you like to set the alarm?")
            hour = raw_input("What hour would you like to set the alarm?: ")
            espeak.synth("What minute would you like to set the alarm?")
            minute = raw_input("What minute would tou like to set the alarm?: ")
            espeak.synth("What would you like me to say after the alarm?")
            message = raw_input("What would you like me to say after the alarm?: ")
            while alarmnow.hour != int(hour) and alarmnow.minute != int(minute):
                    time.sleep(1)
            os.system("omxplayer takemetochurch.mp3")
            espeak.synth(message)
            print(message)

请帮帮我这真的很烦人,我已经工作了这么久,我一辈子都想不出来!你知道吗


Tags: thetoyouinputrawwhatlikeset
2条回答
message = raw_input("What would you like me to say after the alarm?: ")
alarmnow = datetime.datetime.now()
while alarmnow.hour != int(hour) and alarmnow.minute != int(minute):
    time.sleep(1)
    alarmnow = datetime.datetime.now()

你的问题是,你从来没有更新'现在',所以你的时钟将永远轮询。您可以修改while循环以获得每个循环的时间。你知道吗

但你应该尽量避免投票。它效率低下,如果系统中发生了其他事情,您可能会错过窗口。在你的情况下这不太可能,因为你只需要在一分钟内击中。。。除非夏令时来临,你完全错过了。你知道吗

既然你已经有了小时和分钟,而且它们很容易转换成秒,那就睡个好觉吧

if userInput == "set alarm":
            espeak.synth("What hour would you like to set the alarm?")
            hour = raw_input("What hour would you like to set the alarm?: ")
            espeak.synth("What minute would you like to set the alarm?")
            minute = raw_input("What minute would tou like to set the alarm?: ")
            espeak.synth("What would you like me to say after the alarm?")
            message = raw_input("What would you like me to say after the alarm?: ")
            # setup alarm for today but if its already past, go to tomorrow
            now = datetime.datetime.now()
            nominate = datetime.datetime(now.year, now.month, now.day,
                int(hour), int(minute))
            if nominate < now:
                nominate += datetime.timedelta(days=1)
            time.sleep((nominate - now).seconds)
            os.system("omxplayer takemetochurch.mp3")
            espeak.synth(message)
            print(message)

相关问题 更多 >