Python distinguishes itself from other programming languages in a variety of different ways. But there are few elements as visually distinct as Python’s use of whitespace. Most other programming languages use specific characters to open and close code blocks or elements of conditional logic. But Python’s syntax replaces those markers with whitespace. The end effect is far more human-readable code. However, it can at times also result in perplexing errors. And people new to Python, in particular, often encounter a “indentationerror: expected an indented block after ‘for’ statement” error when working with for loops. But as you’ll soon see, this Python error is usually quite easy to fix.
What the Indentation Error Actually Means
The “indentationerror: expected an indented block after ‘for’ statement” is essentially stating that a for loop hasn’t been properly initialized. In many programming languages, you’d set up a for loop with brackets that contain the program logic. But in Python, this is replaced by whitespace. In this context, indentation tells the Python interpreter that the loop’s logic should begin at a certain point. And when that indentation ends the interpreter knows to close the loop. This problem can become further complicated by the inconsistent use of whitespace. And this includes the loop and the larger codebase as a whole. Python coders generally agree that it’s best to always keep all indentation as four characters of whitespace.
A Deeper Dive Into for Loops and Indentation
One of Python’s more unique aspects is how visual it is when compared to other languages. As such, the prior details can be more easily understood with a simple Python script.
ourItems = [“Alpha”, “Beta”, “Gamma”, “Delta”]
for x in ourItems:
print(x)
The Python code begins by creating a small list called ourItems. On the next line we create a for loop that will iterate over the contents of ourItems. And finally, the last line actually prints the contents of ourItems to screen as they’re iterated. Or at least that’s what we want to happen. But instead of printing those items to screen we instead see the “indentationerror: expected an indented block after ‘for’ statement” error message. This is due to the fact that there’s no indentation present after we declare the for loop.
Newcomers to Python often assume that the colon at the end of a for loop functions in a similar way to an opening bracket in other programming languages. If that were true the colon would mark the start of the for loop’s program logic. But what really marks the start of a loop is an indentation. And without indentation, our code simply exits out with an indentation error.
Fixing A for Loop That’s Triggering an Indentation Error
Fixing the error simply requires us to add the proper indentation. Python’s interpreter should have pointed to line 2 as the source of our indentation error. But keep in mind that Python’s interpreter says that it expects an indented block after the for loop on line 2, rather than on line 2. So we need to go down to the line following the error point – line 3. And we can simply add a four character indentation to the beginning of that line to fix the problem. The code should now look like the following example.
ourItems = [“Alpha”, “Beta”, “Gamma”, “Delta”]
for x in ourItems:
print(x)
If you run the newly modified code it should loop over every string in ourItems and print it to screen. This is obviously an extremely simple example. But the same methodology used to fix the indentation error here should work for you just as well with any other code.