Python’s indentation style is arguably its most universally recognized trait. Other languages tend to use curly braces or similar notation to denote a code block. But Python enforces a strict coding style that actually uses whitespace as part of its program logic. But when things go wrong with that process, you’ll see an indentaton error such as ” indentationerror: expected an indented block “. The error is typically easy to fix once you know what to look for. But that also requires a deeper dive into Python indentation.
What Does the Indentationerror Mean?
Python error message synax can be vague at times. But the indentionerror is reletively clear in its description of a problem with your Python code indentation. Python’s syntax uses indentation to separate programming logic and code blocks. When Python expects to see an indented block, but doesn’t, it typically throws this indentationerror.
There are a number of potential explanations for the underlying cause of the error. But it’s most commonly associated with inconsistent spacing when starting a new code block. For example, this might occur when you’re declaring a new function, loop, or a conditional statement. The simple solution is to simply look at the area around the error and manually redo any indentation. However, theory and practice don’t always go hand in hand. And there are instances where you’ll need to look a little deeper into the python error.
A Closer Look at Indentations and Their Associated Errors
It’s best to begin by creating a simple example of the indentationerror. Consider the following demonstration.
def calculateOurValue(initialVal):
calcVal = initialVal*2
return calcVal
print( calculateOurValue(5) )
We begin by defining a function called calculateOurValue. It simply takes a passed variable, multiplies it by two, and then returns the result. Or at least that’s what would happen if the code was working. If you try running it as it is now you’ll simply receive a message stating “Indentationerror: Expected an Indented Block”. The indentation error refers to the fact that line two and three should be part of an indented block below line one. This is easily corrected by simply highlighting those two lines and pressing tab on your keyboard. Running it again afterward will result in the number 10 being printed to screen.
You might wonder about the fact that tabs work as spacing to fix an indented block error. Tabs are generally frowned upon as a spacing indicator in Python due to the fact that they work inconsistently over different environments. But in using a tab we also see one of the interesting aspects of an indenterror. The amount of whitespace doesn’t really matter. What’s important is that there’s never a different amount of space used to denote your programming logic. Go back to the previous example and indent line two and three with a single space. You might be surprised to find that this method works just as well as using a tab key to create space. The same would hold true if you used five, six, ten or any other number of spaces.
The Python interpreter really only insists on consistent white space usage within your code. It’s quite lenient in how you go about using that white space. Though the Python community as a whole generally agrees that indentation levels should be denoted by four spaces. And most Python-focused IDEs will automatically translate the tab key into a four-spaced format.
Fixing the Indentationerror
Fixing a Python indentationerror will usually vary in difficulty based on the nature of the underlying cause. Some errors stem from a simple typo. Others come from inconsistent indentation stemming from the use of different development environments for the same project. But this particular error tends to arise from actual mistakes in your code. This means that automated solutions aren’t very applicable. If you were mixing up tabs and spaces you could do a simple search and replace. But mistakes in code logic need to be fixed by manually looking through the indentation level of your code. Sometimes this will be as easy as noticing that you forgot to add an indentation level to denote a function. But you might also have an indentation level stemming from an attempt to fix other problems. For example, consider the following code.
def calculateOurValue(initialVal):
if initialVal > 0:
print(“Greater than zero”)
calcVal = initialVal*2
return calcVal
print( calculateOurValue(5) )
If someone was quickly scanning through their code for an unexpected indent they might just haphazardly press tab and end up camouflaging errors. In this example, it looks like the earlier problem with the calculateOurValue function has been fixed. But the new conditional checking the initialVal variable introduces another area where problems with indention can sneak in. And, indeed, the print statement below the if conditional should have an additional indentation. But even fixing that problem will highlight the issue with messy fixes.
The function looks fine at first glance. But it’s largely one mass of an unexpected indentation. What seems to be a normal indent actually has extra space. And while consistent white space as an indent is workable in Python, the operative word there is consistent. Fixing it means looking for any extra space or unexpected indent and manually repairing it. Thankfully the Python interpreter is usually fairly good in guessing where the problems come from. You can usually just follow the line numbers to see where the major problems arise from. In the previous example, we’re told that the return doesn’t match the outer indentation level. That’s a clear sign that the logic within the function is largely out of alignment. So we should work downward from the top and fix it like so.
def calculateOurValue(initialVal):
if initialVal > 0:
print(“Greater than zero”)
calcVal = initialVal*2
return calcVal
print( calculateOurValue(5) )
We’ve removed the different amount of spacing that was causing the problem. And the syntax error goes away in the process. Unlike many Python errors, there’s no automated solution that can handle everything for us. But fixing the problem is usually just a matter of looking at the line number specified in the error and working upward from there. As a worst-case scenario, you can always remove all indention from a function. And from that point, you can just manually add indentation as needed to denote the proper program logic.