indentation – What to do with Unexpected indent in python?
indentation – What to do with Unexpected indent in python?
Python uses spacing at the start of the line to determine when code blocks start and end. Errors you can get are:
Unexpected indent. This line of code has more spaces at the start than the one before, but the one before is not the start of a subblock (e.g. if/while/for statement). All lines of code in a block must start with exactly the same string of whitespace. For instance:
>>> def a():
... print foo
... print bar
IndentationError: unexpected indent
This one is especially common when running python interactively: make sure you dont put any extra spaces before your commands. (Very annoying when copy-and-pasting example code!)
>>> print hello
IndentationError: unexpected indent
Unindent does not match any outer indentation level. This line of code has fewer spaces at the start than the one before, but equally it does not match any other block it could be part of. Python cannot decide where it goes. For instance, in the following, is the final print supposed to be part of the if clause, or not?
>>> if user == Joey:
... print Super secret powers enabled!
... print Revealing super secrets
IndendationError: unindent does not match any outer indentation level
Expected an indented block. This line of code has the same number of spaces at the start as the one before, but the last line was expected to start a block (e.g. if/while/for statement, function definition).
>>> def foo():
... print Bar
IndentationError: expected an indented block
If you want a function that doesnt do anything, use the no-op command pass:
>>> def foo():
... pass
Mixing tabs and spaces is allowed (at least on my version of Python), but Python assumes tabs are 8 characters long, which may not match your editor. Just say no to tabs. Most editors allow them to be automatically replaced by spaces.
The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.
In Python, the spacing is very important, this gives the structure of your code blocks.
This error happens when you mess up your code structure, for example like this :
def test_function() :
if 5 > 3 :
print hello
You may also have a mix of tabs and spaces in your file.
I suggest you use a python syntax aware editor like PyScripter, or Netbeans
indentation – What to do with Unexpected indent in python?
Run your code with the -tt option to find out if you are using tabs and spaces inconsistently