其他语法错误 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16474599/
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-18 22:45:22  来源:igfitidea点击:

Else Syntax Error Python

pythonif-statement

提问by user2364549

if len(user_hash) > 0:
  with open(log_file, "w") as log_f:
    for name in user_hash:
        log_f.write("Name:%s \n Email: %s" % (name, email)

    else len(user_hash) < 0:
       print "Nothing happened :("

I keep getting a syntax error on the else statement and I am unsure why it keeps producing this error. I don't have any other else statements in the same def and it still gives error. What do I do?

我一直在 else 语句上收到语法错误,我不确定为什么它会不断产生此错误。我在同一个 def 中没有任何其他 else 语句,它仍然给出错误。我该怎么办?

采纳答案by eluke66

Your log_f.write statement is also missing a trailing ')', which is likely confusing the parser...and the indentation doesn't look right. Cut and paste problem?

您的 log_f.write 语句也缺少尾随的 ')',这可能会使解析器感到困惑……而且缩进看起来不正确。剪切粘贴问题?

回答by schilippe

You can't do elselike that in Python. You should do (assuming your tabs are correct in real life):

else在 Python 中你不能这样做。您应该这样做(假设您的标签在现实生活中是正确的):

elif len(user_hash) < 0:

elif len(user_hash) < 0:

and as mentioned, you're missing a close paren.

如前所述,您缺少一个亲密的父母。

回答by BrenBarn

You can't supply a condition with an elsestatement. elsemeans "everything else" --- that is, everything else but whatever conditions you specified in an earlier if. It's not clear what you're trying to accomplish with that else, but perhaps you mean it to be an if.

您不能在else语句中提供条件。 else意味着“其他一切”——也就是说,除了您在之前指定的任何条件之外的一切if。目前尚不清楚您想用它来完成什么else,但也许您的意思是它是一个if.

It could also be an elif("else if"), but if you mean it to be an elsefor the earlier ifclause, then you need to unindent it so it's at the same indentation level as the if. An ifand it's else/elifhave to line up at the same indentation level.

它也可能是一个elif(“如果其他人”),但如果你的意思是它是一个else为早期的if条款,那么你需要取消缩进它,所以它是在同一缩进层次的if。Anif和 it's else/elif必须以相同的缩进级别排列。

(There is such a thing as an elseclause for a forstatement, but it doesn't look like that's what you want here.)

(有else一个for语句的子句之类的东西,但它看起来不像你想要的那样。)

回答by pradyunsg

In Python, the elsestatement takes no conditions:

在 Python 中,该else语句不接受任何条件:

if condition:
   do_1()
else:
   do_else()

In your case, since you want to evaluate another condition, after the if, use an elif:

在您的情况下,由于您想评估另一个条件,在 之后if,请使用elif

if condition1:
    do_1()
elif condition2:
    do_2()
... # you can have as many elifs as you want
else:
    do_else()

Note: Read the docs.

注意:阅读文档

回答by pradyunsg

You can do elsewith forloops in Python, but you'll need to balance your parentheses on:

您可以在 Python 中使用for循环做其他事情,但您需要平衡括号:

log_f.write("Name:%s \n Email: %s" % (name, email)

which should actually be

这实际上应该是

log_f.write("Name:%s \n Email: %s" % (name, email))

(Note the extra final parenthesis.)

(注意额外的最后括号。)

回答by Mauricio Sierra

if and else must be in the same column align, this work for me.

if 和 else 必须在同一列对齐,这对我有用。