2016-04-24 15 views
-1

私は毎朝私の街の天気を私にメールで知らせる簡単なプログラムに取り組んでいます。現時点では動作しますが、一度だけ動作します。 whileループを使用することで動作しますが、私は持っているので、それが今明らかに whileループを1回だけ使用する

while time == 0600: 
    send my mail etc 

、その分全体のためになるようにそれを作ること、私はメールでスパムを取得、に設定しました。ですから、24時間に1回何か起こる方法を見つけ出す必要があります。

私のフルコードです(現在は再起動するまで、一度しか動作しません)。

import smtplib, pywapi, datetime 
weather = True 
loopmsg = True 
loopmsg1 = True 
def send(): 
    loopmsg = True 
    loopmsg1 = True 
    global weather 
    while weather == True: 
     if loopmsg == True: 
      print('Initial Loop Initiated') 
      loopmsg = False 
     time = datetime.datetime.now() 
     time = str(time) 
     time = time[11:] 
     time = time[:-10] 
     time = time.replace(":", "") 
     time = int(time) 
     fromaddr = 'xxx' 
     toaddrs = 'xxx' 
     while time == 0600: 
      print('Time is correct') 
      weather_com_result = pywapi.get_weather_from_weather_com('ASXX0075') 
      msg = "It is " + weather_com_result['current_conditions']['text'].lower() + " and " + weather_com_result['current_conditions']['temperature'] + "°C in Your City." 
      msg = msg.encode('utf-8') 

      # Credentials (if needed) 
      username = 'xxx' 
      password = 'xxx' 

      # The actual mail send 
      server = smtplib.SMTP('smtp.gmail.com:587') 
      server.starttls() 
      server.login(username,password) 
      server.sendmail(fromaddr, toaddrs, msg) 
      server.quit() 
      print('Sent') 
      #weather = False 

    #while weather == False: 
     # if loopmsg1 == True: 
     # print('Second Loop Initiated') 
     # loopmsg1 = False 
     # while time > 0600: 
      # send() 

send() 
+1

なぜ 'while'、' if'は1回だけ実行するのですか?とにかく:1. 'sendMail'フラグを追加し、sendルーチン内で' True'に設定します。 2. 'sentMail'が' True'のときは送信しません。 3. 'sentMail'を' False'にリセットする新しい 'if time == 0601'を追加します。 – usr2564301

答えて

0

なぜ電子メールを送信した直後にbreakステートメントがありますか?これはちょうどあなたがループから脱出する原因になります。その後、プログラムの残りの部分を実行します。

while time == 0600: 
     print('Time is correct') 
     weather_com_result = pywapi.get_weather_from_weather_com('ASXX0075') 
     msg = "It is " + weather_com_result['current_conditions']['text'].lower() + " and " + weather_com_result['current_conditions']['temperature'] + "°C in Your City." 
     msg = msg.encode('utf-8') 

     # Credentials (if needed) 
     username = 'xxx' 
     password = 'xxx' 

     # The actual mail send 
     server = smtplib.SMTP('smtp.gmail.com:587') 
     server.starttls() 
     server.login(username,password) 
     server.sendmail(fromaddr, toaddrs, msg) 
     server.quit() 
     print('Sent') 
     break 
2

まず、1日に1回だけ何かをするためにスクリプトを実行しています。これは非合理です。スクリプトは毎日6時にアクティブになるように、あなたのOS(Win、Linux、Mac)にタスクをスケジュールする必要があります。スクリプト内の時間条件を削除します。

テレグラムボットを作成し、携帯電話でいつでも指定した場所のメッセージを送信するようにしてください。

しかし、このスクリプトは簡単に修正できます。そのwhileループをifとして使用しています。一度だけ電子メールを送信する変数を追加するだけです。

if time == 0500: 
    send_email = True 
if send_email and time == 0600: 
    print('Time is correct') 
    send_email = False 
    weather_com_result = pywapi.get_weather_from_weather_com('ASXX0075') 
    .... 
関連する問題