IndentationErrors serve two purposes: they help make your code more readable and ensure the Python interpreter correctly understands your code. If you add in an additional space or tab where one is not needed, you’ll encounter an “IndentationError: unexpected indent” error.
In this guide, we discuss what this error means and why it is raised. We’ll walk through an example of this error so you can figure out how you can fix it in your program.
IndentationError: unexpected indent
An indent is a specific number of spaces or tabs denoting that a line of code is part of a particular code block. Consider the following program:
def hello_world(): print("Hello, world!")
We have defined a single function: hello_world()
. This function contains a print statement. To indicate to Python this line of code is part of our function, we have indented it.
You can indent code using spaces or tabs, depending on your preference. You should only indent code if that code should be part of another code block. This includes when you write code in:
Python code must be indented consistently if it appears in a special statement. Python enforces indentation strictly.
Some programming languages like JavaScript do not enforce indentation strictly because they use curly braces to denote blocks of code. Python does not have this feature, so the language depends heavily on indentation.
The cause of the “IndentationError: unexpected indent” error is indenting your code too far, or using too many tabs and spaces to indent a line of code.
The other indentation errors you may encounter are:
- Unindent does not match any other indentation level
- Expected an indented block
An Example Scenario
We’re going to build a program that loops through a list of purchases that a user has made and prints out all of those that are greater than $25.00 to the console.
To start, let’s define a list of purchases:
purchases = [25.50, 29.90, 2.40, 57.60, 24.90, 1.55]
Next, we define a function to loop through our list of purchases and print the ones worth over $25 to the console:
def show_high_purchases(purchases): for p in purchases: if p > 25.00: print("Purchase: ") print(p)
The show_high_purchases()
function accepts one argument: the list of purchases through which the function will search. The function iterates through this list and uses an if
statement to check if each purchase is worth more than $25.00.
If a purchase is greater than $25.00, the statement Purchase:
is printed to the console. Then, the price of that purchase is printed to the console. Otherwise, nothing happens.
Before we run our code, call our function and pass our list of purchases as a parameter:
show_high_purchases(purchases)
Let’s run our code and see what happens:
File "main.py", line 7 print(p) ^ IndentationError: unexpected indent
Our code does not run successfully.
The Solution
As with any Python error, we should read the full error message to see what is going on. The problem appears to be on line 7, which is where we print the value of a purchase.
if p > 25.00: print("Purchase: ") print(p)
We have incidentally indented the second print()
statement. This causes an error because our second print()
statement is not part of another block of code. It is still part of our if
statement.
To solve this error, we need to make sure that we consistently indent all our print()
statements:
if p > 25.00: print("Purchase: ") print(p)
Both print()
statements should use the same level of indentation because they are part of the same if
statement. We’ve made this revision above.
Let’s try to run our code:
Purchase: 25.5 Purchase: 29.9 Purchase: 57.6
Our code successfully prints out all the purchases worth more than $25.00 to the console.
Conclusion
“IndentationError: unexpected indent” is raised when you indent a line of code too many times. To solve this error, make sure all of your code uses consistent indentation and that there are no unnecessary indents.
Now you’re ready to fix this error like a Python expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.