Python 中的缩进错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14979224/
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
Indentation Error in Python
提问by sharkbait
I can't compile because of this part in my code:
由于我的代码中的这一部分,我无法编译:
if command == 'HOWMANY':
opcodegroupr = "A0"
opcoder = "85"
elif command == 'IDENTIFY':
opcodegroupr = "A0"
opcoder = "81"
I have this error:
我有这个错误:
Sorry: IndentationError: ('unindent does not match any outer indentation level', ('wsn.py', 1016, 30, "\t\telif command == 'IDENTIFY':\n"))
抱歉: IndentationError: ('unindent 不匹配任何外部缩进级别', ('wsn.py', 1016, 30, "\t\telif command == 'IDENTIFY':\n"))
But I don't see any indentation error. What can be the problem?
但我没有看到任何缩进错误。可能是什么问题?
采纳答案by Udo Klein
In doubt change your editor to make tabs and spaces visible. It is also a very good idea to have the editor resolve all tabs to 4 spaces.
有疑问,请更改您的编辑器以使选项卡和空格可见。让编辑器将所有制表符解析为 4 个空格也是一个很好的主意。
回答by Martijn Pieters
You are mixing tabs and spaces.
您正在混合制表符和空格。
Find the exact location with:
使用以下命令查找确切位置:
python -tt yourscript.py
and replace alltabs with spaces. You really want to configure your text editor to only insert spaces for tabs as well.
并更换所有用空格选项卡。您确实希望将文本编辑器配置为仅为制表符插入空格。
回答by replay
Did you maybe use some <tab>instead of spaces?
你可能使用了一些<tab>而不是空格吗?
Try remove all the spaces before the code and readd them using <space>characters, just to be sure it's not a <tab>.
尝试删除代码前的所有空格并使用<space>字符读取它们,以确保它不是<tab>.
回答by PSL1988
In Notepad++
在记事本++中
View --->Show Symbols --->Show White Spaces and Tabs(select)
查看 ---> 显示符号 ---> 显示空格和制表符(选择)
replace all tabs with spaces.
用空格替换所有制表符。
回答by Rahul Satal
For Sublime Text Editor
对于 Sublime 文本编辑器
Indentation Error generally occurs when the code contains a mix of both tabs and spaces for indentation. I have got a very nice solution to correct it, just open your code in a sublime text editor and find 'Tab Size' in the bottom right corner of Sublime Text Editor and click it. Now select either
当代码包含用于缩进的制表符和空格的混合时,通常会发生缩进错误。我有一个非常好的解决方案来纠正它,只需在 sublime 文本编辑器中打开您的代码,然后在 sublime 文本编辑器的右下角找到“标签大小”并单击它。现在选择
'Convert Indentation to Spaces'
OR
或者
'Convert Indentation to Tabs'
Your code will work in either case.
您的代码在任何一种情况下都可以使用。
回答by Vicrobot
It happened to me also, but I got the problem solved. I was using an indentation of 5 spaces, but when I pressed tab, it used to put a four space indent. So I think you should just use one thing; i.e. either tab button to add indent or spaces. And an ideal indentation is one of 4 spaces. I found IntelliJ to be very useful for these sort of things.
这也发生在我身上,但我解决了问题。我使用了 5 个空格的缩进,但是当我按下 Tab 键时,它曾经放置了一个 4 个空格的缩进。所以我认为你应该只使用一件事;即要么选项卡按钮添加缩进或空格。理想的缩进是 4 个空格之一。我发现 IntelliJ 对这类事情非常有用。
回答by ojass
This has happened with me too, python is space sensitive,
so after " :"(colon)
you might have left a space,
for example:
[space is represented by "."]
这也发生在我身上,python 对空间敏感,所以在“ :”(冒号)之后你可能会留下一个空格,
例如:[空格由“ .”表示]
`if command == 'HOWMANY':.
opcodegroupr = "A0"
opcoder = "85"
elif command == 'IDENTIFY':.
opcodegroupr = "A0"
opcoder = "81"`
so try removing the unnecessary spaces,if you open it in IDE your cursor will be displayed away from ":" something like :- "if command == 'HOWMANY': |"
....whereas it should be:- "if command == 'HOWMANY':|"
所以尝试删除不必要的空格,如果你在 IDE 中打开它,你的光标将显示在远离 ":" 的地方,类似于 :- " if command == 'HOWMANY': |"
.... 而它应该是:- " if命令 == 'HOWMANY':|"

