Today, I wanted to kick off a potential new series on Python errors. Up first, I figured I’d start by exploring a syntax error. Specifically, the syntax error that reads either
SyntaxError: expected an indented block or
IndentationError: expected an indented block.
In short, this SyntaxError occurs when a new block is opened but no code is found. For example, when a loop or conditional is declared, the colon indicates a new block. If there isn’t any code in that block, this error will get thrown.
In the remainder of this article, we’ll talk about what a SyntaxError is and how this specific syntax error might arise with examples.
Table of Contents
What Is a SyntaxError?
If we stop to look at the type of error first, we’ll find that we’re dealing with a SyntaxError. In Python, a SyntaxError is any error in which the interpreter cannot make sense of the code. In other words, the fundamental structure of the code cannot be interpreted.
Often, a SyntaxError will arise when a piece of code doesn’t follow Python syntax. For example, if we were to try to run a piece of Java code in Python, we’ll likely find that we’ll get a SyntaxError:
>>> int x = 10; SyntaxError: invalid syntax
Clearly the proper way to write this line of code would be to remove the type or use the correct type annotation syntax:
>>> x = 10 >>> x: int = 10
Of course, sometimes, the Python interpreter is capable of giving more detailed information about a SyntaxError. In the next section, we’ll take a look at our specific error message:
expected an indented block.
What Does This SyntaxError Message Mean?
In general, syntax errors can be thought of as a broad class of errors. As a result, there are a lot of different ways they can manifest. In our case, we’re working with a SyntaxError in the context of code blocks. Specifically, our error reads:
expected an indented block.
As a result, this error arises when a code block is opened using one of four different syntax elements:
- If Statements
- Function Definitions
- Class Definitions
Naturally, I’ll share an example of all four scenarios in which this error would be thrown:
if True: # SyntaxError: expected an indented block x = 5
while True: # SyntaxError: expected an indented block x = 5
def syntax_error_example(): # SyntaxError: expected an indented block x = 5
class SyntaxErrorExample: # SyntaxError: expected an indented block x = 5
In each of these examples, I added a little line to the end. Otherwise, we’d get a slightly different but related error:
SyntaxError: unexpected EOF while parsing. Regardless, they both tell us that our code blocks are missing content.
On a side note, when I tried reproducing this error in different environments (e.g. IDLE, PyCharm, JDoodle, etc.), I sometimes received the SyntaxError I referenced before, and other times I received an IndentationError. Of course, the message is always the same:
expected an indented block. Fortunately, as it turns out, IndentationError is a subclass of SyntaxError, so they’re effectively the same thing:
Base class for syntax errors related to incorrect indentation. This is a subclass ofPython 3 Documentation (05/07/2020)
In the next section, we’ll look at how we can address this error.
How to Fix This SyntaxError?
One of the nice things about this Syntax error is that it tells us exactly what it expects:
expected an indented block. In other words, we need to put something in the new block we just opened.
Now, it’s impossible for me to assume what your code looks like, but I imagine that your getting this error because you assumed an empty block would do nothing. Unfortunately, that’s not the case in Python. However, we can accomplish the same thing using
pass. Here are all the same examples reworked:
if True: pass x = 5
while True: pass x = 5
def syntax_error_example(): pass x = 5
class SyntaxErrorExample: pass x = 5
Now, suddenly we have working code! Of course, any code will work to fix up this error. For example, we might decide that each of these code snippets should do more than nothing:
if True: print("Plus Ultra!") x = 5
while True: break x = 5
def syntax_error_example(): return "Evgeni Malkin" x = 5
class SyntaxErrorExample: def __init__(self): self.y = 10 x = SyntaxErrorExample().y
In either case, all of these code snippets run without a SyntaxError.
Need Help Fixing This SyntaxError?
To recap, the error we’re dealing with today is a type of SyntaxError which arises when a code block is empty. To fix it, we need to populate our empty code block.
If this article didn’t help you figure out how to fix your own issue, I’m sure the community would be happy to help! Head on over to Twitter and share your code snippet (preferably with Carbon) under #RenegadePython.
Feel free to piggyback off this sample tweet:
Otherwise, that’s all we have for today! If you’d like to show this site some love, head on over to my list of ways to grow the site. In addition, you’re welcome to stick around and read some of these related articles:
Finally, here are some resources from the folks at Amazon (ad):
Thanks again for hanging out. I appreciate the support!
After over three years of grinding toward a PhD, I have finally passed my qualifying exam. It's time to celebrate! What Is a Qualifying Exam? For people who choose to pursue a PhD, there is a...
Looking to bring your static GitHub profile to life? Look no further than this quick tutorial.