2

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
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
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Christopher Long
  • 51
  • 1
  • 1
  • 2

5 Answers5

10

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!

Gio
  • 3,242
  • 1
  • 25
  • 53
-1

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

  • Nothing in the question mentions Visual Studio. You should create your own question (and possibly also answer it) when having a problem with a more specific environment. – sampi Jul 21 '22 at 06:01
-1

For linux nano users:

if your code includes 4 spaces instead of a tab, you should keep using 4 spaces or change all of them to a tab. If you use mixed it gives an error.

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

skm
  • 1
  • 1
    My text editor doesn't have a Format menu! And how do you know all editors' indentation commands will make tabs consistent? – Nathan Tuggy Jun 29 '17 at 18:57
  • I believe all code editors have some feature like this. It might be called "reindent", "auto indent" or somthing like that. – Håken Lid Jun 29 '17 at 19:06
  • I used the leafpad text editor and this is how I was able to fix the error. It will either make your indents be all tabs or all spaces – skm Jun 30 '17 at 13:03
-2

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.

zaerymoghaddam
  • 3,037
  • 1
  • 27
  • 33