Tab Error in Python

ぃ、小莉子 提交于 2019-11-26 22:11:31

问题


The following python code throws this error message, and I can't tell why, my tabs seem to be in line:

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

回答1:


You cannot mix tabs and spaces, according the PEP8 styleguide:

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 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!




回答2:


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.




回答3:


not using the backspace also is important (especially in the Leafpad editor). If you want to decrease indent, use only Alt_key + TAB.

This should be done when you delete a line in a Python code.



来源:https://stackoverflow.com/questions/28325553/tab-error-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!