Python 我收到一个缩进错误。我如何解决它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45621722/
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
I'm getting an IndentationError. How do I fix it?
提问by Christian Dean
I have a Python script:
我有一个 Python 脚本:
if True:
if False:
print('foo')
print('bar')
However, when I attempt to run my script, Python raises an IndentationError
:
但是,当我尝试运行我的脚本时,Python 会引发一个IndentationError
:
File "script.py", line 4
print('bar')
^
IndentationError: unindent does not match any outer indentation level
I kept playing around with my program, and I was also able to produce three other errors:
我一直在玩我的程序,我还能够产生其他三个错误:
IndentationError: unexpected indent
IndentationError: expected an indented block
TabError: inconsistent use of tabs and spaces in indentation
IndentationError: unexpected indent
IndentationError: expected an indented block
TabError: inconsistent use of tabs and spaces in indentation
What do these errors mean? What am I doing wrong? How can I fix my code?
这些错误是什么意思?我究竟做错了什么?我该如何修复我的代码?
回答by Christian Dean
Why does indentation matter?
为什么缩进很重要?
In Python, indentation is used to delimit blocks of code. This is different from many other languages that use curly braces {}
to delimit blocks such as Java, Javascript, and C. Because of this, Python users must pay close attention to when and how they indent their code because whitespace matters.
在 Python 中,缩进用于分隔代码块。这{}
与 Java、Javascript 和 C等许多其他使用花括号分隔块的语言不同。因此,Python 用户必须密切注意他们何时以及如何缩进代码,因为空格很重要。
When Python encounters a problem with the indentation of your program, it either raises an exception called IndentationError
or TabError
.
当 Python 遇到程序缩进问题时,它会引发一个名为IndentationError
或的异常TabError
。
A little history
一点历史
The historical reasons for why Python uses indentation vs the arguably more commonly accepted curly braces {}
is outlined in an article of the history of Python by Guido van Rossum- the creator of Python:
Python的创建者Guido van Rossum{}
在一篇关于 Python 历史的文章中概述了Python 使用缩进与可以说更普遍接受的花括号的历史原因:
Python's use of indentation comes directly from ABC, but this idea didn't originate with ABC--it had already been promoted by Donald Knuth and was a well-known concept of programming style. (The occam programming language also used it.) However, ABC's authors did invent the use of the colon that separates the lead-in clause from the indented block. After early user testing without the colon, it was discovered that the meaning of the indentation was unclear to beginners being taught the first steps of programming. The addition of the colon clarified it significantly: the colon somehow draws attention to what follows and ties the phrases before and after it together in just the right way.
Python 对缩进的使用直接来自 ABC,但这个想法并非起源于 ABC——它已经被 Donald Knuth 推广,是一个众所周知的编程风格概念。(occam 编程语言也使用了它。)但是,ABC 的作者确实发明了使用冒号将导入子句与缩进块分开。在没有冒号的早期用户测试之后,发现缩进的含义对于学习编程的第一步的初学者来说是不清楚的。添加冒号显着地澄清了这一点:冒号以某种方式将注意力吸引到后面的内容并以正确的方式将其前后的短语联系在一起。
How do I indent my code?
如何缩进我的代码?
The basic rule for indenting Python code (considering that you treat the entire program as a "basic block") is: The first statement in a basic block, and each subsequent statement after it must be indented by the same amount.
Python 代码缩进的基本规则(考虑到您将整个程序视为“基本块”)是: 基本块中的第一条语句,其后的每个语句都必须缩进相同的数量。
So technically the following Python program is correct:
所以从技术上讲,以下 Python 程序是正确的:
def perm(l):
# Compute the list of all permutations of l
if len(l) <= 1:
return [l]
r = []
for i in range(len(l)):
s = l[:i] + l[i+1:]
p = perm(s)
for x in p:
r.append(l[i:i+1] + x)
return r
However, as you can probably tell from above, randomly indenting your code makes is extremely hard to read and follow the flow of the program. It's better to be consistent and follow a style.
但是,正如您可能从上面看到的那样,随机缩进您的代码使得阅读和遵循程序流程非常困难。最好保持一致并遵循一种风格。
PEP8 - The Python style guide - recommends that four spaces per indentation levelshould be used:
PEP8 - Python 风格指南 - 建议每个缩进级别应该使用四个空格:
Use 4 spaces per indentation level.
每个缩进级别使用 4 个空格。
That is, each statement that is starting a new block and each subsequent statement in the new block, should be indented four spaces from the current indentation level. Here is the above program indented according to the PEP8 style guide:
也就是说,开始新块的每个语句和新块中的每个后续语句都应该从当前缩进级别缩进四个空格。这是根据 PEP8 样式指南缩进的上述程序:
def perm(l):
# Compute the list of all permutations of l
if len(l) <= 1:
return [l]
r = []
for i in range(len(l)):
s = l[:i] + l[i+1:]
p = perm(s)
for x in p:
r.append(l[i:i+1] + x)
return r
Can I still use tabs?
我还可以使用标签吗?
Python realizes that some people still prefer tabs over spaces and that legacy code may use tabs rather than spaces, so it allows the use of tabs as indentation. PEP8 touches on this topic:
Python 意识到有些人仍然更喜欢制表符而不是空格,并且遗留代码可能使用制表符而不是空格,因此它允许使用制表符作为缩进。PEP8 涉及这个话题:
Spaces are the preferred indentation method.
Tabs should be used solely to remain consistent with code that is already indented with tabs.
空格是首选的缩进方法。
制表符应仅用于与已使用制表符缩进的代码保持一致。
Note however the one big caveat is not to use both tabs andspaces for indentation. Doing so can cause all kinds of strange hard to debug indentation errors. Python expands tabs to the next 8th column, but if your editor is set to a tab size of 4 columns, or you you use spaces as well as tabs, you can easily produce indented code that looksfine in your editor, but Python will refuse to run. The Python 3 compiler explicitlyrejects any program containing an ambiguous mixture of tabs and spaces, usually by raising a TabError
. However, by default, mixing tabs and spaces is still allowed in Python 2, but it is highly recommended not to use this "feature". Use the -t
and -tt
command line flags to force Python 2 to raise a warning or (preferably) an error respectively. PEP8 also discusses this topic:
但是请注意,最大的警告是不要同时使用制表符和空格进行缩进。这样做会导致各种奇怪的难以调试的缩进错误。Python 将制表符扩展到下一个第 8 列,但是如果您的编辑器设置为 4 列的制表符大小,或者您使用空格和制表符,您可以轻松生成在编辑器中看起来不错的缩进代码,但 Python 会拒绝跑步。Python 3 编译器明确拒绝任何包含不明确的制表符和空格混合的程序,通常通过提高TabError
. 但是,默认情况下,Python 2 中仍然允许混合使用制表符和空格,但强烈建议不要使用此“功能”。使用-t
和-tt
命令行标志来强制 Python 2 分别引发警告或(最好)错误。PEP8 也讨论了这个话题:
Python 3 disallows mixing the use of tabs and spaces for indentation.
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
Python 3 不允许混合使用制表符和空格进行缩进。
使用制表符和空格混合缩进的 Python 2 代码应转换为仅使用空格。
使用 -t 选项调用 Python 2 命令行解释器时,它会发出有关非法混合制表符和空格的代码的警告。使用 -tt 时,这些警告会变成错误。强烈推荐这些选项!
What does "IndentationError: unexpected indent" mean?
“IndentationError:意外缩进”是什么意思?
Problem
问题
This error occurs when a statement is unnecessarily indented or its indentation does not match the indentation of former statements in the same block. For example, the first statement in the program below is unnecessarily indented:
当语句不必要地缩进或其缩进与同一块中先前语句的缩进不匹配时,会发生此错误。例如,下面程序中的第一条语句不必要地缩进:
>>> print('Hello') # this is indented
File "<stdin>", line 1
print('Hello') # this is indented
^
IndentationError: unexpected indent
In this example, the can_drive = True
line in the if
block does not match the indentation of any former statement:
在此示例中,块中的can_drive = True
行if
与任何先前语句的缩进都不匹配:
>>> age = 10
>>> can_drive = None
>>>
>>> if age >= 18:
... print('You can drive')
... can_drive = True # incorrectly indented
File "<stdin>", line 3
can_drive = True # incorrectly indented
^
IndentationError: unexpected indent
Fix
使固定
The fix for this error is to first make sure the problematic line even needs to be indented. For example, the above example using print
can be fixed simply be unindenting the line:
解决此错误的方法是首先确保有问题的行甚至需要缩进。例如,上面使用的示例print
可以简单地固定为取消缩进行:
>>> print('Hello') # simply unindent the line
Hello
However, if you are sure the line does need to be indented, the indentation needs to match that of a former statement in the same block. In the second example above using if
, we can fix the error by making sure the line with can_drive = True
is indented at the same level as the former statements in the if
body:
但是,如果您确定该行确实需要缩进,则缩进需要与同一块中前一个语句的缩进相匹配。在上面使用 的第二个示例中if
,我们可以通过确保 with 与正文中can_drive = True
前面的语句在同一级别缩进来修复错误if
:
>>> age = 10
>>> can_drive = None
>>>
>>> if age >= 18:
... print('You can drive')
... can_drive = True # indent this line at the same level.
...
What does "IndentationError: expected an indented block" mean?
“IndentationError:应为缩进块”是什么意思?
Problem
问题
This error occurs when Python sees the 'header' for a compound statement, such as if <condition>:
or while <condition>:
but the compound statement's body or blockis never defined. For example in the code below we began an if
statement, but we never define a body for the statement:
当 Python 看到复合语句的“标题”时会发生此错误,例如if <condition>:
orwhile <condition>:
但从未定义复合语句的主体或块。例如,在下面的代码中,我们开始了一条if
语句,但我们从未为该语句定义主体:
>>> if True:
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
In this second example, we began writing a for
loop, but we forget to indent the for
loop body. So Python still expects an indented block for the for
loop body:
在第二个例子中,我们开始编写一个for
循环,但我们忘记缩进for
循环体。所以 Python 仍然希望for
循环体有一个缩进的块:
>>> names = ['sarah', 'lucy', 'michael']
>>> for name in names:
... print(name)
File "<stdin>", line 2
print(name)
^
IndentationError: expected an indented block
Comments don't count as bodies:
评论不算作正文:
>>> if True:
... # TODO
...
File "<stdin>", line 3
^
IndentationError: expected an indented block
Fix
使固定
The fix for this error is to simply include a body for the compound statement.
此错误的修复方法是简单地包含复合语句的主体。
As shown above, a common mistake by new users is that they forget to indent the body. If this is the case, make sure each statement meant to be included in the compound statement's body is indented at the same level under the compound statement's beginning. Here is the above example fixed:
如上所示,新用户的一个常见错误是他们忘记缩进正文。如果是这种情况,请确保要包含在复合语句正文中的每个语句都在复合语句开头的同一级别缩进。这是上面修复的示例:
>>> names = ['sarah', 'lucy', 'michael']
>>> for name in names:
... print(name) # The for loop body is now correctly indented.
...
sarah
lucy
michael
Another common case is that, for some reason, a user may not want to define an actual body for the compound statement, or the body may be commented out. In this case, the pass
statement can be used. The pass
statement can be used anywhere Python expects one or more statements as a placeholder. From the documentation for pass
:
另一种常见情况是,由于某种原因,用户可能不想为复合语句定义实际的主体,或者主体可能被注释掉。在这种情况下,pass
可以使用该语句。该pass
语句可以在 Python 需要一个或多个语句作为占位符的任何地方使用。从文档中pass
:
pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example:
def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet)
pass 是一个空操作——当它被执行时,什么也不会发生。当语法上需要语句但不需要执行代码时,它可用作占位符,例如:
def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet)
Here is the above example with the if
statement fixed by using the pass
keyword:
这是上面if
使用pass
关键字修复语句的示例:
>>> if True:
... pass # We don't want to define a body.
...
>>>
What does "IndentationError: unindent does not match any outer indentation level" mean?
“IndentationError: unindent 不匹配任何外部缩进级别”是什么意思?
Problem
问题
This error occurs when you unindent a statement, but now the indentation level of that statement does not match that of any former statement. For example, in the below code we unindent the second call to print
. However, the indentation level does not match that of any former statement:
当您取消一个语句的缩进时会发生此错误,但现在该语句的缩进级别与任何先前语句的缩进级别都不匹配。例如,在下面的代码中,我们取消对print
. 但是,缩进级别与任何以前的语句都不匹配:
>>> if True:
... if True:
... print('yes')
... print()
File "<stdin>", line 4
print()
^
IndentationError: unindent does not match any outer indentation level
This error is especially hard to catch because even one space will cause your code to fail.
这个错误特别难以捕捉,因为即使是一个空格也会导致您的代码失败。
Fix
使固定
The fix is to ensure that when you unindent a statement, the indentation level matches that of a former statement. Consider the above example once again. In the example, I want the second call to print to be in the first if
statements body. So I need to make sure that that line's indentation level matches that of the former statements in the first if
statement's body:
解决方法是确保当您取消语句缩进时,缩进级别与前一个语句的缩进级别相匹配。再次考虑上面的例子。在示例中,我希望第二个打印调用位于第一个if
语句正文中。所以我需要确保该行的缩进级别与第一个if
语句正文中的前一个语句的缩进级别匹配:
>>> if True:
... if True:
... print('yes')
... print() # indentation level now matches former statement's level.
...
yes
>>>
I'm still getting an IndentationError but my program appears to be correctly indented. What do I do?
我仍然收到 IndentationError 但我的程序似乎已正确缩进。我该怎么办?
If your program visually appears to have correct indentation, but your still getting an IndentationError
you have most likely mixed tabs with spaces. This will sometimes cause Python to raises strange errors. See the subsection Special casesunder What does "TabError: inconsistent use of tabs and spaces in indentation" mean?for an more in-depth explanation of the problem.
如果您的程序在视觉上看起来有正确的缩进,但您仍然得到一个IndentationError
很可能是混合制表符和空格。这有时会导致 Python 引发奇怪的错误。请参阅“TabError:缩进中制表符和空格的不一致使用”是什么意思下的特殊情况小节?以更深入地解释问题。
What does "TabError: inconsistent use of tabs and spaces in indentation" mean?
“TabError:在缩进中使用不一致的制表符和空格”是什么意思?
Problem
问题
This error only occurs when you attempt to mix tabs and spaces as indentation characters. As said above, Python will not allow your program to contain a mix of tabs and spaces, and will raise the specific exception TabError
if it finds you have. For example, in the program below, a mix of tabs and spaces is used for indentation:
仅当您尝试将制表符和空格混合作为缩进字符时,才会发生此错误。如上所述,Python 不允许你的程序包含制表符和空格的混合,TabError
如果它发现你有,就会引发特定的异常。例如,在下面的程序中,混合使用制表符和空格来缩进:
>>> if True:
... if True:
... print()
... print()
... print()
File "<stdin>", line 5
print()
^
TabError: inconsistent use of tabs and spaces in indentation
Here is a picture which visually shows the whitespace in the above program. Gray dots are spaces, and gray arrows are tabs:
这是一张图片,直观地显示了上述程序中的空白。灰色点是空格,灰色箭头是制表符:
We can see we have indeed mixed spaces and tabs for indentation.
我们可以看到我们确实混合了空格和制表符来缩进。
Special cases
特别案例
Note Python will notalwaysraise a TabError
if you mix tabs and spaces into your program. If the program indentation is unambiguous, Python will allow tabs and spaces to be mixed. For example:
注意如果将制表符和空格混合到程序中,Python不会总是引发 a TabError
。如果程序缩进是明确的,Python 将允许混合使用制表符和空格。例如:
>>> if True:
... if True: # tab
... pass # tab, then 4 spaces
...
>>>
And sometimes Python simply chokes on the mixture of tabs and spaces and erroneously raises an IndentationError
exception when a TabError
would be more appropriate. Another example:
有时 Python 只是因为制表符和空格的混合而窒息,并IndentationError
在 aTabError
更合适时错误地引发异常。另一个例子:
>>> if True:
... pass # tab
... pass # 4 spaces
File "<stdin>", line 3
pass # 4 spaces
^
IndentationError: unindent does not match any outer indentation level
As you can see, running your code this way can create mysterious errors. Even though the program visuallyappears to be fine, Python became confused trying to parse the tabs and spaces used for indention and errored out.
如您所见,以这种方式运行代码会产生神秘的错误。尽管该程序在视觉上看起来不错,但 Python 在尝试解析用于缩进的制表符和空格时变得困惑并出错。
These are excellent examples that demonstrate why to never mix tabs and spaces and make use of the -t
and -tt
interpreter flags when using Python 2.
这些都是证明为什么从不混合制表符和空格和利用的很好的例子-t
,并-tt
使用Python 2时解释标志。
Fix
使固定
If your program is short, probably the easiest and quickest fix is to simply re-indent the program. Make sure each statement is indented by four spaces per indention level (see How do I indent my code?).
如果您的程序很短,可能最简单快捷的解决方法就是简单地重新缩进程序。确保每个语句在每个缩进级别缩进四个空格(请参阅如何缩进我的代码?)。
However, if you already have a large program that you've mixed tabs and spaces into, there are automated tools that can be used to convert all of your indentation to just spaces.
但是,如果您已经有一个混合制表符和空格的大型程序,则可以使用自动化工具将所有缩进转换为空格。
Many editors such as PyCharmand SublimeTexthave options to automatically convert tabs to spaces. There are also several on-line tools such as Tabs To Spacesor Browserlingthat allow you to quickly re-indent your code. There are also tools written in Python. autopep8for example can automatically re-indent your code and other indentation errors as well.
许多编辑器(如PyCharm和SublimeText)都有自动将制表符转换为空格的选项。还有一些在线工具,例如Tabs To Spaces或Browserling,可让您快速重新缩进代码。也有用 Python 编写的工具。例如autopep8可以自动重新缩进您的代码和其他缩进错误。
Even the best tools though will sometimes not be able to fix all of your indentation errors and you'll have to fix them manually. That's why it's important to always properly indent your code from the start.
即使最好的工具有时也无法修复所有缩进错误,您必须手动修复它们。这就是为什么从一开始就正确缩进代码很重要的原因。
A note about "SyntaxError" related indentation problems
关于“SyntaxError”相关缩进问题的说明
Although not often, sometimes certain SyntaxError
exceptions are raised due to incorrect indentation. For example, look at the code below:
尽管不常见,但有时会SyntaxError
由于缩进不正确而引发某些异常。例如,看看下面的代码:
if True:
pass
pass # oops! this statement should be indented!.
else:
pass
When the above code is run, a SyntaxError is raised
:
当上面的代码运行时,一个SyntaxError is raised
:
Traceback (most recent call last):
File "python", line 4
else:
^
SyntaxError: invalid syntax
Although Python raises a SyntaxError
, the realproblem with the above code, is that the second pass
statement should be indented. Because the second pass
isn't indented, Python doesn't realize that the previous if
statement and the else
statement are meant to be connected.
尽管 Python 提出了SyntaxError
,但上述代码的真正问题在于第二个pass
语句应该缩进。因为第二个pass
没有缩进,Python 没有意识到前一个if
语句和该else
语句是要连接的。
The fix for this type of error is to simply correctly re-indent your code. To see how to properly indent your code, see the section How do I indent my code?.
修复此类错误的方法是简单地正确地重新缩进您的代码。要了解如何正确缩进代码,请参阅如何缩进代码?.
I'm still having a hard time with Python's indentation syntax. What do I do?
我仍然很难使用 Python 的缩进语法。我该怎么办?
Don't get discouraged if you're still struggling. It can take time to get use to Python's whitespace syntax rules. Here are some tips to help:
如果你还在挣扎,不要气馁。习惯 Python 的空格语法规则可能需要一些时间。以下是一些有用的提示:
- Get an editor that will tell you when you have an indentation error. Some goods ones are as said above are, PyCharm, SublimeText, and Jupyter Notebook.
- When you indent your code, count out loud to yourself how many times you press the space-bar (or tab key). For example, if you needed to indent a line by four spaces, you would say out loud "one, two, three, four" while simultaneously pressing the space-bar each time. It sounds silly, but it helps train your brain to think about how deep you're indenting your code.
- If you have an editor, see if it has an option to automatically convert tabs to spaces.
- View others' code. Browse githubor Stackoverflowand see examples of Python code.
- Just write code. That's the single best way to get better. The more you write Python code, the better you'll get.
- 获取一个编辑器,当您遇到缩进错误时会告诉您。上面提到的一些商品是PyCharm、SublimeText和Jupyter Notebook。
- 当你缩进你的代码时,大声数出你按下空格键(或 Tab 键)的次数。例如,如果您需要将一行缩进四个空格,您可以大声说“一、二、三、四”,同时每次按下空格键。这听起来很傻,但它有助于训练你的大脑思考代码缩进的深度。
- 如果您有编辑器,请查看它是否具有自动将制表符转换为空格的选项。
- 查看其他人的代码。浏览github或Stackoverflow并查看 Python 代码示例。
- 只写代码。这是变得更好的唯一最好方法。你写的 Python 代码越多,你就会得到越好。
Resources used
使用的资源
回答by chebaby
Sublime Text 3
崇高的文字 3
If it happens that you code in Sublime Text 3, this could help you with indentations problemes
如果碰巧您在 Sublime Text 3 中编码,这可以帮助您解决缩进问题
In Sublime Text, while editing a Python file:
在 Sublime Text 中,编辑 Python 文件时:
Sublime Text menu> Preferences> Settings - Syntax Specific:
Sublime Text 菜单>Preferences>Settings - Syntax Specific:
Python.sublime-settings
Python.sublime-settings
{
"tab_size": 4,
"translate_tabs_to_spaces": true
}
回答by The Coder
You see, you have a tiny error.
你看,你有一个小错误。
if True:
if False:
print('foo')
print('bar')
You were supposed to do:
你应该这样做:
if True:
if False:
print('foo')
print('bar')
As you can see your print is only indented 3 spaces, it is supposed to be indented 4 spaces.
如您所见,您的印刷品仅缩进了 3 个空格,它应该缩进 4 个空格。
回答by Matthew Park
Quick fix for Sublime users:
Sublime 用户的快速修复:
- Press Ctrl-H to access Find and Replace
- In Find: Type 4 spaces
- In Replace: Copy and paste a tab from somewhere in your code Click Replace All
- 按 Ctrl-H 访问查找和替换
- 在查找中:键入 4 个空格
- 在替换中:从代码中的某处复制并粘贴选项卡 单击全部替换