Experienced programmers are often shocked by the clarity of Python code when they see it for the first time. Most programs written in Python adhere to solid style guidelines and are generally extremely easy to read. And this is in large part due to the fact that Python’s interpreter uses indentation as programming logic. But the fact that the system forces you to use specific style guidelines can also open up the possibility for errors like ” indentationerror: expected an indented block after function definition “. Thankfully, solving this error generally just comes down to understanding how and why Python is so insistent on proper indentation.
Python Has a Unique Approach to Whitespace
Python uses indentation to signal most types of code blocks. This includes conditional statements, methods, and functions. And in the case of this Python error, we’re being notified of an indention problem with a function. The most common explanation is that the Python interpreter has found a function whose internal logic uses the same indentation as its declaration. This is usually solved by simply providing consistent indentation for all of a function’s code. In short, you probably just need to indent the code immediately after a function’s declaration.
A More In-Depth Look at Functions and Indentation
This error is a lot easier to understand by looking at an example, given the visual nature of Python’s program logic. The following code offers a simple example of the most common trigger for the ” indentationerror: expected an indented block after function definition ” problem.
calculated = ourInitialNumber*ourInitialNumber
The code beings by declaring a function called ourNumberMachine. In line 2, we multiply the variable passed to ourNumberMachine and assign it to a new variable called calculated. We return the calculated value at the end of the function. And, finally, we make a call to ourNumberMachine within a print statement.
However, if you run the code you’ll see the indentionerror. Take special note of the fact that the error message points to both line 1 and 2 as the problem. This is where people new to Python’s formatting might become confused. Both lines 1 and 2 are correctly formatted in terms of Python’s syntax. And in other contexts, these lines might be perfectly fine. The problem is how they interact with each other.
When Python’s interpreter points to a problem in line 1 it’s actually saying that the contents of this function are an issue. Namely, the code following line 1 needs to be an indented block within the larger context of ourNumberMachine. Line 2 is shown as a problem, but not because of any incorrect syntax. Rather, line 2 is incorrect simply due to the fact that it should be the start of a function’s program logic but instead shares its indentation level. Again, remember that Python uses indentation to signify a code block. This is equivalent to opening or closing brackets in most other programming languages. With all of that in mind, how would we go about fixing this problem?
How To Fix the Indention Error
Bring up the prior example again and consider what was highlighted in the error messages. We see that line 2 triggers the error with its very first character in the variable declaration. This is usually a hint that we’re looking at a problem with Python’s formatting rather than specific syntax. Python’s interpreter generally doesn’t care what we name our variables. When it points out a problem with a variable declaration, independent of variable type, it tends to be an indentation issue. Line 1 is also highlighted as a root cause of the indentation error.
So we can start by looking at line 1 and 2. And the answer can then be seen from the relationship between these two lines. Each has the same indentation level. But a code block within a function needs to have one level of indentation below the actual declaration of the function. This is easily fixed in most text editors by simply highlighting all of the unindented code in the ourNumberMachine function and pressing tab. We now have something like the following code.
calculated = ourInitialNumber*ourInitialNumber
This might seem like a lot of analysis for what amounts to a two-line function. But for better or worse, that’s the nature of indentation errors. A lot of Python’s errors can be fixed with automated solutions or scripts. But indentation errors are generally only fixed by analyzing the area of the problem.
Real-world situations will obviously be more complex than the simple example here. But the actual process you use to solve them is typically the same. It’s just a matter of using the line numbers provided by the interpreter as something similar to coordinates on a map. They can essentially allow you to triangulate a problem area in your code. And when you’re familiar with Python’s indentation system, that’s usually enough to show you exactly where you need to indent a code block.