只是爲了說明我在展示代碼之前正在處理的項目背景。我目前正在開發一個Python腳本,該腳本將在Raspberry Pi上運行,以監視地下室中的水池泵的浮子開關。此代碼將檢查是否該排水泵不被這兩個標準的工作:Python無效語法循環
- 如果交換機上超過三分鐘
- 如果開關開啓和關閉的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非常陌生,這是我的第一個真正的項目。我不熟悉很多語法,所以這可能是一個非常基本的錯誤。任何人都可以請幫我修復這個聲明的語法,以便我可以讓我的代碼工作。我知道沒有其他代碼的情況下還有許多其他的錯誤,但是當我找到它們時,我正計劃解決這些錯誤。我已經搜索過,但我找不到像這樣的另一個例子。任何和所有的幫助非常感謝!
你」在這段時間之後,我獲得了無與倫比的elif。我認爲你的意思是隻是一個if。 – pburka