. Advertisement .
..3..
. Advertisement .
..4..
Compared to other languages, Python has many unique error messages like “unexpected indent”. This tutorial will explain why it occurs and the solutions for the error.
The “Unexpected Indent” Error In Python
The actual error message Python produces, which most of us run into at least once while learning and programming in this programming language:
IndentationError: unexpected indent
The IndentationError exception is a subclass of SyntaxError. It is triggered when Python parses an indentation that isn’t supposed to be there.
This is a pure syntax error – the kind that beginners usually make. For instance, this is a common mistake when writing an if-else statement in Python:
a = 1;
if a:
print("The statement is true")
print(a)
else:
print("The statement is false")
print(a)
And it will leave you with this message:
File "code.py", line 5
print(a)
IndentationError: unexpected indent
To understand why it happens and how you can avoid this error, you will need to learn about indentation in Python and its rules.
Indentation In Python
What is Indentation?
While many other programming languages like Java, HTML, or C++ use curly braces {} to mark code blocks, Python relies on indentation.
This is one of the things that make Python unique but also requires attention to detail from learners. It doesn’t exist for readability purposes. In Python, indentation allows Python to read and interpret lines of code correctly.
Indentation is whitespaces (tabs or spaces) that begin a line of code. You must use it to start a code block (such as the body of an if statement or a function).
As a programmer, you are free to choose the way tabs or spaces are used to make an indentation. But it must be presented to specify a code block.
You can, of course, write the entire block in a single line using semicolons. However, it is always a better idea to indent it instead. Compare these two snippets, which are both valid and do the same thing.
Snippet 1:
a = 28
b = 22
if a > b:
print("a is greater than b") # a is greater than b
Snippet 2:
a = 28
b = 22
if a > b: print("a is greater than b") # a is greater than b
The first writing style is more easy to follow and understand, meaning maintenance and debugging are much easier.
Indentation Rules
Python uses the indentation level to determine which lines of code belong to the same group. The language’s official code style, PEP8, says that you need to use four spaces to mark an indentation. This is a good practice if you want to keep your code consistent with how the built-in libraries are written.
However, you can also use a tab or eight spaces for this purpose. It depends on both personal preference and your project’s requirements. Just make sure not to insert unnecessary indentation to avoid the “unexpected indent” error.
You can style each code block differently (even though it is a bad programming practice). Python still executes them just fine. But it will throw the IndentationError exception if there are several indentation levels in a single block of code.
Final Words
Indentation is a distinctive and important feature in Python. Without a good understanding of it, you may run into common issues like the “unexpected indent” error. It is easy to fix this, however, as you can reformat your code block to remove the error message.
Leave a comment