Python while 循环语法无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19082315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:48:18  来源:igfitidea点击:

Python while loop with invalid syntax

pythonwhile-loop

提问by cmbrooks

Just to give some background on the project I am working on before I show the code. I am currently developing a Python script that will be running on a Raspberry Pi to monitor the float switch from a sump pump in my basement. This code will check to see if the sump pump is not working by these two criteria:

在我展示代码之前,只是提供一些我正在处理的项目的背景知识。我目前正在开发一个 Python 脚本,该脚本将在 Raspberry Pi 上运行,以监控我地下室污水泵的浮子开关。此代码将检查污水泵是否不符合以下两个标准:

  1. If the switch is on for more than three minutes
  2. If the switch turns on and off more than 10 times in three minutes
  1. 如果开关打开超过三分钟
  2. 如果开关在三分钟内打开和关闭超过 10 次

I am not done with the rest of the code, but here is what I have:

我还没有完成其余的代码,但这是我所拥有的:

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

My problem is that on line 52 when it says

我的问题是在第 52 行,当它说

while floatSwitch is True:

There is an error in the code, and all it says is 'invalid syntax' I am very new to Python, and this is my first real project with it. I am unfamiliar with a lot of the syntax, so this could be a very elementary mistake. Can anyone please help me fix the syntax of this statement so that I can get my code to work. I know that there are numerous other mistakes without the rest of the code, but I am planning to work out those bugs when I find them. I have searched around, but I cannot find another example like this. Any and all help is very appreciated!

代码中有一个错误,它说的是“无效语法”我对 Python 非常陌生,这是我第一个真正使用它的项目。我不熟悉很多语法,所以这可能是一个非常基本的错误。任何人都可以帮我修复此语句的语法,以便我的代码可以正常工作。我知道在没有其余代码的情况下还有许多其他错误,但是我计划在找到这些错误时解决它们。我四处搜索,但找不到这样的另一个例子。非常感谢任何和所有帮助!

采纳答案by Rohit Jain

Actually, your problem is with the line abovethe while-loop. You are missing a parenthesis:

实际上,您的问题出在 while 循环上方的那一行。您缺少一个括号:

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

Also, just a tip for the future, instead of doing this:

另外,只是对未来的一个提示,而不是这样做:

while floatSwitch is True:

it is cleaner to just do this:

这样做更干净:

while floatSwitch:

回答by Rohit Jain

You have an unbalanced parenthesis on your previous line:

您在上一行中有一个不平衡的括号:

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

And also in sendEmail()method, you have a missing opening quote:

而且在sendEmail()方法中,您缺少开头引用:

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