Python IndentationError
IndentationError: expected an indented block
Python uses the spaces at the start of a line (indentation) to decide what is inside an if or a for. You get this error when those spaces are missing, are there when they should not be, or do not line up with each other. With this error, not a single line of your program runs. Look at the leading spaces on the line that the number after line in the error message points to.
Cause 1. expected an indented block: the inner line is not indented
A line that ends with a colon (:), such as if, for, while or def, must be followed by at least one indented line. The after 'if' statement on line 2 at the end of the error message means the if on line 2 has no inner line (this part appears from Python 3.10 on).
Wrong code
x = 5if x > 3:print("big") File "main.py", line 3
print("big")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 2Fixed code
Put 4 spaces in front of the inner line.
If you have nothing to put inside yet, add a line with pass.
Cause 2. unexpected indent: a line is indented where it should not be
You get this when a line starts with spaces but does not come after a line ending in a colon. It often happens when you copy and paste code from somewhere else and the leading spaces come along with it.
Wrong code
name = "Mina" print(name) File "main.py", line 2
print(name)
IndentationError: unexpected indentFixed code
Delete the spaces in front of that line so it lines up with the line above.
Cause 3. unindent does not match: the indentation matches no earlier line
When you reduce the indentation to step back out, the line must have the same number of spaces as some earlier line. Below, the last line has 2 spaces, but the lines above have only 0 or 4.
Wrong code
x = 5if x > 3: print("big") print("done") File "main.py", line 4
print("done")
^
IndentationError: unindent does not match any outer indentation levelFixed code
Use 4 spaces if the line belongs inside the if, or 0 if it belongs outside.
Cause 4. TabError: tabs and spaces are mixed
They may look the same on screen, but one tab character and 4 spaces are different. Mixing the two inside one block gives TabError: inconsistent use of tabs and spaces in indentation. TabError is a kind of IndentationError. The last line of the code below is indented with a tab.
Wrong code
x = 5if x > 3: print("big") print("done") File "main.py", line 4
print("done")
^
TabError: inconsistent use of tabs and spaces in indentationFixed code
Stick to one of them. Most people use 4 spaces. Delete the indentation at the start of the line and put it in again.
What to check when you see this error
- The number after
linein the error message is the line with the problem. The error message also shows the code on that line. - For
expected an indented block, check that the line above ends with a colon and that the line below it is indented. - For
unexpected indent, delete the spaces in front of that line. - For
unindent does not matchorTabError, delete the indentation on that line and the lines above and below it, then put it back with 4 spaces. - Indentation can be valid and still change the meaning. Choose it by whether the line should run inside the loop or outside it.