非常简单的 Python 上的无效语法 if ... else 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14327195/
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
Invalid syntax on VERY SIMPLE Python if ... else statement
提问by blueuser
Can someone explain why I am getting an invalid syntax error from Python's interpretor while formulating this simple if...else statement? I don't add any tabs myself I simply type the text then press enter after typing. When I type an enter after "else:" I get the error. "Else" is highlighted by the interpreter. What's wrong?
有人可以解释为什么我在制定这个简单的 if...else 语句时从 Python 的解释器中得到无效的语法错误吗?我自己不添加任何选项卡,我只是输入文本,然后在输入后按 Enter。当我在“else:”之后输入回车时,出现错误。“Else”由解释器突出显示。怎么了?
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48)
[MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> if 3 > 0:
print("3 greater than 0")
else:
SyntaxError: invalid syntax
>>>
采纳答案by Isaac Dontje Lindell
Python does not allow empty blocks, unlike many other languages (since it doesn't use braces to indicate a block). The passkeyword must be used any time you want to have an empty block (including in if/else statements and methods).
与许多其他语言不同,Python 不允许空块(因为它不使用大括号来表示块)。pass任何时候您想拥有一个空块时都必须使用该关键字(包括在 if/else 语句和方法中)。
For example,
例如,
if 3 > 0:
print('3 greater then 0')
else:
pass
Or an empty method:
或者一个空的方法:
def doNothing():
pass
回答by Ashwini Chaudhary
That's because your elsepart is empty and also not properly indented with the if.
那是因为您的else部分是空的,并且也没有正确缩进if.
if 3 > 0:
print "voila"
else:
pass
In python passis equivalent to {}used in other languages like C.
在pythonpass中相当于{}在其他语言如C中使用。
回答by Volatility
The elseblock needs to be at the same indent level as the if:
该else块需要与 处于相同的缩进级别if:
if 3 > 0:
print('3 greater then 0')
else:
print('3 less than or equal to 0')
回答by Sugumar
The keyword elsehas to be indented with respect to the ifstatement respectively
关键字else必须if分别相对于语句缩进
e.g.
例如
a = 2
if a == 2:
print "a=%d", % a
else:
print "mismatched"
回答by thirdangle


The problem is indent only.
问题只是缩进。
You are using IDLE. When you press enter after first print statement indent of else is same as print by default, which is not OK. You need to go to start of else sentence and press back once. Check in attached image what I mean.
您正在使用空闲。当您在第一条打印语句后按回车时,else 的缩进默认与打印相同,这是不行的。您需要转到 else 语句的开头并按回一次。检查附加图像我的意思。
回答by rajarajan
it is a obvious mistake we do, when we press enter after the if statement it will come into that intendation,try by keeping the else statement as straight with the if statement.it is a common typographical error
这是我们犯的一个明显错误,当我们在 if 语句之后按 Enter 键时,它会进入该意图,请尝试将 else 语句与 if 语句保持一致。这是一个常见的印刷错误
回答by Jason
Else needs to be vertically aligned. Identation is playing a key role in Python. I was getting the same syntax error while using else. Make sure you indent your code properly
否则需要垂直对齐。标识在 Python 中扮演着关键角色。我在使用 else 时遇到了相同的语法错误。确保正确缩进代码

