Python’s syntax is similar in some respects to other popular programming languages. But when experienced programmers first encounter Python they’re usually struck by the lack of brackets and braces to denote conditional logic. Python instead uses whitespace to signify those elements. This notation system lends itself to extremely clean code. But it can take a while for programmers to become accustomed to Python’s style. And most people learning about Python will encounter a “indentationerror: expected an indented block” error message at some point. But the error’s easy to fix once you understand why it’s been triggered in the first place.
A Concise Look at the Indentation Error
A conditional in Python is created by stating the properties being tested. The next line needs to have an indentation to show that it’s part of the decision-making tree created by the initial conditional declaration. This particular indentation error occurs when the line following a conditional isn’t indented. The solution is usually as simple as just indenting the first line after the conditional pointed to by the error message.
Looking a Little Deeper Into Conditionals and Whitespace
The Python error might still sound rather complex at this point. But the ease in fixing this problem can be seen with a quick example.
a=1
b=2
if b < a:
print(“Less”)
The code begins by creating two variables and assigning them a value of 1 and 2. The next line tests whether the value of b is less than a. If that’s the case, then it should print “Less”. However, if you run this code it will instead produce the “indentationerror: expected an indented block” error message. Interestingly enough this holds true if we expand the conditional’s logic. Consider the following expansion on the prior example.
a=1
b=2
if b < a:
print(‘Less’)
else:
print(“More”)
This Python code shows that the conditional is executed from a top-down view. The very first line of a code block in Python will determine the indentation level of everything to follow within a block. And if that first line isn’t properly indented then the program flow can’t continue past that point.
How To Fix the Indentation Error
Thankfully this error is usually extremely easy to fix. Python’s interpreter will usually give you an exact line number to begin with. But keep in mind that the line number isn’t where the error occurs. Python’s interpreter is instead pointing out the location in your code that needs to be followed by an indentation. With the previous example, the error message points to the if conditional in line 4.
So to fix the error we simply go down by one line and indent it. Try typing four spaces at the beginning of line 5. If you run it again you’ll see that the error has now changed. Instead of pointing to line 4 it will now point to line 6. One important point to keep in mind is that the error is now referring to a problem with indentation after the else statement. Conditionals are generally executed in a hierarchical fashion. The new else conditional is considered the primary focal point rather than the if conditional it’s a part of. Now add four spaces to the beginning of line 7, before the print statement. The final code should look like the following.
a=1
b=2
if b > a:
print(‘Less’)
else:
print(“More”)
Consistency of indentation is normally a vitally important part of Python. But keep in mind that the if and else conditionals are treated as a unique case. Therefore you could use four spaces on line five and two spaces on line seven and the code would still run without any errors. But if you added a new line following either of the print statements then they would have to share the same indentation level as the first line in the conditional. This is one of the areas where Python lets you get away with some sloppiness that is generally best avoided. You should instead always use four spaces for every level of your indentation. And that’s true whether error messages are forcing that on you or not. It leads to cleaner code and fewer chances for unexpected errors further down the line.