1

私はプログラミングがまったく初めてです。この基本的な目覚まし時計を 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)   
4

2 に答える 2

1

この簡単な例を試すことができます。

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()

afterthreadingを開くために使用します。あなたの例では、ユーザーに時間と分を尋ねます。このようにして、 を使用して2つの時間の差を計算します。webbrowsern secondshours and minutes

さらに説明が必要な場合は、コメントしてください。

于 2015-11-26T13:41:49.947 に答える
0

現在のコードは、設定されたアラーム時間に対して現在の時間をチェックしているように見えます。ただし、現在、チェックは 1 つしかありませんが、ループして現在の時刻と設定されたアラーム時刻を継続的に比較するか、継続的にチェックする別の方法を使用する必要があります。

webbrowser 行は動作します。現在の時刻がアラーム時刻に到達しないため (現在の時刻と設定されたアラーム時刻を継続的に比較していないため)、実行されていません。

試す:

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

webbrowser.open(alarm_sound, new=2) #Added temporarily to test webbrowser functionality

if str(now.hour) == alarm_h and str(now.minute) == alarm_m: #Converted integer type to string type to match datatype of alarm_h and alarm_m
    webbrowser.open(alarm_sound, new=2)

これにより、Web ブラウザーが開き、その機能をテストできます。

また、 now.hournow.minuteを文字列型に変換して、 alarm_halarm_mのデータ型に一致させました。どちらも整数であり、文字列データ型と直接比較することはできません。

現在の時刻を継続的に更新し、それが現在のスレッドと等しいかどうかを確認するために、ループまたはスレッドを必ず調べてください。

于 2015-11-26T13:10:14.770 に答える