12

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"))

But I don't see any indentation error. What can be the problem?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
sharkbait
  • 2,980
  • 16
  • 51
  • 89

7 Answers7

16

You are mixing tabs and spaces.

Find the exact location with:

python -tt yourscript.py

and replace all tabs with spaces. You really want to configure your text editor to only insert spaces for tabs as well.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Or the other way around… (depends on your personal preference) – poke Feb 20 '13 at 11:55
  • btw, does this turn tabs into 4 spaces or 8? – dmg Feb 20 '13 at 11:55
  • `-tt` turns mixing tabs and spaces into an error rather than change anything in your code. – akaIDIOT Feb 20 '13 at 11:57
  • @poke: PEP-8 recommends spaces. Using tabs leads to a never-ending debate about using 4 or 8 spaces per tab, and it's *much easier* to accidentally mix the two if you use *some* spaces here and there to align lines. – Martijn Pieters Feb 20 '13 at 11:59
  • 1
    @MartijnPieters If you use tabs, you have tabs, so you do not need to care about its visual presentation. You should never mix tabs and spaces, but apart from that, just *choose one and stick to it*. You are right, it’s a never-ending debate; it totally depends on your personal preference—hence my comment. – poke Feb 20 '13 at 12:02
  • 1
    I have never understood why you would want to use spaces instead of tabs - 1 tab is 1 level of indent and then the size of that is a display preference - but it seems the world disagrees with me. – neil Feb 20 '13 at 12:02
  • 1
    @poke: That's very nice, but in any decent-sized project you will not be the only developer. As soon as you have two people together, there is a large chance you'll disagree about tab size. And pretending that noone will ever make the mistake of mixing tabs and spaces is sticking your head in the sand, frankly. There is a reason that every major style guide for OSS (python or otherwise) states you need to use spaces *only*. :-) – Martijn Pieters Feb 20 '13 at 12:13
  • @MartijnPieters Any decent-sized project (or even small projects; implicitely even one-person projects) will have some preset guidelines on that topic. So again, it was *chosen once* and then all contributors *stick to it*. PEP8 is just one example of such a guideline for many Python related projects (especially the standard library etc.). And your reasoning does not really work in favor of spaces. If the guide said “use tabs only” it would be exactly the same, just with the spaces-guys breaking things. So again: It does not matter, it’s really just a personal preference. – poke Feb 20 '13 at 12:19
  • 1
    There should be one, and preferably only one, obvious way to do it. Following the style of the python codebase itself is obvious. – Wooble Feb 20 '13 at 12:22
12

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.

Udo Klein
  • 6,784
  • 1
  • 36
  • 61
  • What editor can I use? – sharkbait Feb 20 '13 at 12:04
  • My editor is geany under CentOs 6 – sharkbait Feb 20 '13 at 12:06
  • 2
    @sharkbait In its preferences, choose “Editor”/”Display” and there enable “Show whitespace”. Then tabs and spaces will be displayed as some (different) characters. If the lines in question have different characters as indentation, then you have to fix those. ([Source](http://www.geany.org/manual/current/index.html#display)) – poke Feb 20 '13 at 12:10
6

For Sublime Text Editor

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

'Convert Indentation to Spaces'

OR

'Convert Indentation to Tabs'

Your code will work in either case.

Additionally, if you want Sublime text to do it automatically for you for every code you can update the Preference settings as below:-

Sublime Text menu > Preferences > Settings - Syntax Specific :

Python.sublime-settings

{
    "tab_size": 4,
    "translate_tabs_to_spaces": true
}
Rahul Satal
  • 2,107
  • 3
  • 32
  • 53
4

In Notepad++

View --->Show Symbols --->Show White Spaces and Tabs(select)

replace all tabs with spaces.

PSL1988
  • 57
  • 4
2

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.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Vicrobot
  • 3,795
  • 1
  • 17
  • 31
1

Did you maybe use some <tab> instead of spaces?

Try remove all the spaces before the code and readd them using <space> characters, just to be sure it's not a <tab>.

poke
  • 369,085
  • 72
  • 557
  • 602
replay
  • 3,569
  • 3
  • 21
  • 30
0

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

`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':| "

ojass
  • 19
  • 1