Python中的选项卡错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28325553/
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
Tab Error in Python
提问by Christopher Long
The following python code throws this error message, and I can't tell why, my tabs seem to be in line:
以下 python 代码抛出此错误消息,我不知道为什么,我的选项卡似乎是一致的:
File "test.py", line 12
pass
^ TabError: inconsistent use of tabs and spaces in indentation
My code:
我的代码:
class eightPuzzle(StateSpace):
StateSpace.n = 0
def __init__(self, action, gval, state, parent = None):
StateSpace.__init__(self, action, gval, parent)
self.state = state
def successors(self) :
pass
回答by Gio
You cannot mix tabs and spaces, according the PEP8 styleguide:
根据PEP8 样式指南,您不能混合使用制表符和空格:
Spaces are the preferred indentation method.
空格是首选的缩进方法。
Tabs should be used solely to remain consistent with code that is already indented with tabs.
制表符应仅用于与已使用制表符缩进的代码保持一致。
Python 3 disallows mixing the use of tabs and spaces for indentation.
Python 3 不允许混合使用制表符和空格进行缩进。
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.
使用制表符和空格混合缩进的 Python 2 代码应转换为仅使用空格。
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!
使用 -t 选项调用 Python 2 命令行解释器时,它会发出有关非法混合制表符和空格的代码的警告。使用 -tt 时,这些警告会变成错误。强烈推荐这些选项!
回答by skm
open your code in a text editor, highlight all of it (ctr+a) and go to format and select either "Tabify region" or "Untabify region". It'll just make all the indents have the same format.
在文本编辑器中打开您的代码,突出显示所有代码(ctr+a)并转到格式并选择“Tabify region”或“Untabify region”。它只会使所有缩进具有相同的格式。
回答by Eranga De Silva
not using the backspace also is important (especially in the Leafpad editor). If you want to decrease indent, use only Alt_key + TAB
.
不使用退格键也很重要(尤其是在 Leafpad 编辑器中)。如果要减少缩进,请仅使用Alt_key + TAB
.
This should be done when you delete a line in a Python code.
这应该在您删除 Python 代码中的一行时完成。
回答by Sujit Bhattacharyya
Using Visual Studio 2019
使用 Visual Studio 2019
I was using tabs but the editor was inserting spaces and it would result in errors.
我正在使用制表符,但编辑器正在插入空格,这会导致错误。
To avoid getting the spaces and tabs mixed up , set your preferences
为避免混淆空格和制表符,请设置您的首选项
Go to Edit->Advanced->Set Leading Whitespace->Tabs (or Whitespaces)
转到编辑-> 高级-> 设置前导空格-> 制表符(或空格)
After I set it to Tabs, my tabs stop being represented as spaces and it worked fine thereafter
将其设置为制表符后,我的制表符不再表示为空格,此后工作正常