gpt4 book ai didi

python - 非常基本的闹钟打不开网页浏览器

转载 作者:太空宇宙 更新时间:2023-11-03 17:12:40 25 4
gpt4 key购买 nike

我对编程完全陌生。想要用 Python 编写这个基本的闹钟,但网络浏览器打不开。我认为这可能是我的 if 语句不起作用。那是对的吗?

from datetime import datetime
import webbrowser

name = raw_input("What's your name?")
print ("Hello %s! Let me set an alarm for you. Please answer the following questions about when you want to wake up.")%(name)
alarm_h = raw_input("--> Please enter the hour when I should wake you up:")
alarm_m = raw_input("--> Please enter the exact minute of the hour:")
alarm_sound = raw_input("--> Please enter the Youtube-URL of your favorite song:")

now = datetime.today()

print ("It's now %s h : %s m. We'll wake you up at %s h : %s m." %(now.hour, now.minute, alarm_h, alarm_m))

if now.hour == alarm_h and now.minute == alarm_m:
webbrowser.open(alarm_sound, new=2)

最佳答案

您可以尝试这个简单的示例。

from datetime import datetime
import webbrowser
import threading


name = raw_input("What's your name?")
print ("Hello %s! Let me set an alarm for you. Please answer the following questions about when you want to wake up.")%(name)
alarm_h = raw_input("--> Please enter the hour when I should wake you up:")
alarm_m = raw_input("--> Please enter the exact minute of the hour:")
alarm_sound = raw_input("--> Please enter the Youtube-URL of your favorite song:")
print alarm_sound
now = datetime.today()

def test():
webbrowser.open(alarm_sound)

s1 = '%s:%s'
FMT = '%H:%M'
tdelta = datetime.strptime(s1% (alarm_h, alarm_m), FMT) - datetime.strptime(s1%(now.hour, now.minute), FMT)

l = str(tdelta).split(':')
ecar = int(l[0]) * 3600 + int(l[1]) * 60 + int(l[2])
print ecar

threading.Timer(ecar, test).start()

我们使用线程n秒后打开网络浏览器。在您的示例中,您询问用户小时和分钟,这样我们仅使用小时和分钟来计算两个时间之间的差异。

如果您需要更多解释,请评论。

关于python - 非常基本的闹钟打不开网页浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33939307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com