2013-09-29 137 views
0

只是爲了說明我在展示代碼之前正在處理的項目背景。我目前正在開發一個Python腳本,該腳本將在Raspberry Pi上運行,以監視地下室中的水池泵的浮子開關。此代碼將檢查是否該排水泵不被這兩個標準的工作:Python無效語法循環

  1. 如果交換機上超過三分鐘
  2. 如果開關開啓和關閉的10倍以上三種分鐘

我沒有與代碼的其餘部分完成的,但這裏是我有:

import time 

import RPi.GPIO as GPIO 
GPIO.setmode(GPIO.BCM) 
GPIO.setup(17, GPIO.IN) 
floatSwitch = GPIO.input(17) 

import smtplib 

running = True 
log = open("sumpPumpLog.txt", "r+") 
startTime = time.time() 


def elapsedTime(): 
    """This function checks how much time 
    has elapsed since the timer has started""" 
    endtime = time.time() 
    elapsed = endtime - starttime 
    return elapsed 


def sendEmail(*msg): 
    """This function sends an email to selected recipients with a custom 
    message as well as the log file attached.""" 
    #enter the code that sends an email to the family with the log attached 

    fromaddr = '[email protected]' 
    toaddrs = [[email protected]'] 
    msg = """Please see the Pi and the data log file for more details.""" 

    # Credentials (if needed) 
    username = 'my_username' 
    password = 'my_password' 

    msg.attached() 

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


if running is True: 
    if floatSwitch is True: 
     #Write the time and what happened to the file 
     log.write(str(time.time() + "Float switch turned on") 
     #Wait until switch is turned off 

     while floatSwitch is True: 
      startTime = time.time() 
      if floatSwitch is False: 
       log.write(str(now) + "Float switch turned off") 
       break 
     #if elapsedTime > 3 min (in the form of 180 seconds) 
     elif elapsedTime() > 180: 
      log.write(str(now) + "Sump Pump has been deemed broaken") 
      sendEmail("The sump pump is now broken.") 

else: 
    log.write(str(time.time() + "The sctipt has stopped.") 
    sendEmail("The script has been stopped.") 

我的問題是,行52當它說

while floatSwitch is True: 

代碼有錯誤,它說的是'無效的語法' 我對Python非常陌生,這是我的第一個真正的項目。我不熟悉很多語法,所以這可能是一個非常基本的錯誤。任何人都可以請幫我修復這個聲明的語法,以便我可以讓我的代碼工作。我知道沒有其他代碼的情況下還有許多其他的錯誤,但是當我找到它們時,我正計劃解決這些錯誤。我已經搜索過,但我找不到像這樣的另一個例子。任何和所有的幫助非常感謝!

+0

你」在這段時間之後,我獲得了無與倫比的elif。我認爲你的意思是隻是一個if。 – pburka

回答

3

其實,你的問題是與線以上 while循環。你缺少一個括號:

log.write(str(time.time() + "Float switch turned on")) 
               here--^ 

而且,只是對未來的一個提示,而不是做這樣的:

while floatSwitch is True: 

它是清潔劑只是這樣做:

while floatSwitch: 
+0

感謝您的幫助! – cmbrooks

2

你在以前的線路上有一個不平衡的括號:

log.write(str(time.time() + "Float switch turned on")) # Last parenthesis is missing 

而且還sendEmail()方法,你有一個丟失的開引號:

toaddrs = [[email protected]'] # Opening quote missing 
+0

@hcwhsa。沒有看到:) –

+0

感謝您的幫助! – cmbrooks