1
votes

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
4
Did you use tabs for one line, and spaces for the other? You can configure you editor to always use 4 spaces when you tab to be consistent. - dannymilsom
Please format your code with the class declaration in the code block. - Malik Brahimi
I'm willing to bet, that you didn't indent your instance methods under the class declaration. - Malik Brahimi
I think the easiest solution is: don't use tabs. - Andrea Corbellini

4 Answers

8
votes

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!

0
votes

Using 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

-1
votes

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.

-2
votes

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.