Как исправить ошибку IndentationError: unindent does not match any outer indentation level?
Когда пытаюсь запустить, ошибка в строке 41 ( y += (jumpCount ** 2) / 2 ):
- Вопрос задан более двух лет назад
- 21477 просмотров
Простой 3 комментария
- Вконтакте

- Вконтакте
Перепроверьте сколько пробклов в каждой строчке. Нет ли там табов?
В питоне нет фигурных скобок, как в си, или begin/end, как в паскале. Содержимое блока (if/for) выделяется отступом.
IndentationError: unindent does not match any outer indentation level
The IndentationError: unindent does not match any outer indentation level error occurs when you use both spaces and tabs to indent in your code. You can define a code block with any amount of indent, but the indent must match exactly to be at the same level. The IndentationError: unindent does not match any outer indentation level error indicates that there is an excess indent in the line that the python interpreter does not expect to have. The unindent code does not match with any outer indentation. To fix the issue, make sure the code contains either spaces or tabs.
In python, the error IndentationError: unindent does not match any outer indentation level occurs when you add excess indents at the beginning of the line. The compound statement and functions requires the indent to be distinguished from other lines. If the indent does not match with any of the outer indentation level, the error will be thrown.
The indent is defined as the distance or number of empty spaces between the start of the line and the left margin of the line. Indents are not considered in the most recent programming languages such as java, c++, dot net, etc. Python uses the indent to distinguish compound statements and user defined functions from other ..more…
How to Solve Python IndentationError: unindent does not match any outer indentation level
Python uses indentation to define the scope and extent of code blocks in constructs like class, function, conditional statements and loops. You can use both spaces and tabs to indent your code, and if you use both methods when writing your code, you will raise the error: IndentationError: unindent does not match any outer indentation level.
We will go through the error in detail and an example to learn how to solve it.
Table of contents
IndentationError: unindent does not match any outer indentation level
What is Indentation in Python?
Indentation is vital for constructing programs in Python. It refers to the correct use of white space to start a code block. With indentations, we can quickly identify the beginning and endpoint of any code block in our program. Let’s look at how indentation in Python works visually:

To indicate a code block in Python, you must indent each block line by the same amount. You can use four spaces or one tab, a typical indentation for Python. According to the conventions in PEP 8, four white spaces is preferable. You can use indentation to nest code blocks within code blocks.
Python objects if you use both spaces and tabs to indent your code. You need to use one form of indentation, and this can be tricky because you cannot see the difference between spaces and tabs.
The error commonly occurs when you copy code from other sources to your script. The code you are copying may have a different indentation to what you are using.
The error can also occur if you have used indentation in the wrong place or have not used any indentation.
Example: Mixing Indentation in Function
Let’s write a program that calculates the square roots of a list of numbers and prints the result to the console. We will start by defining the function to calculate the square root of a number:
The function uses a for loop to iterate through every number in the list you will pass. We use the exponentiation operator to calculate the square root of the number and then print the result. Next, we will define the list of numbers and then call the get_square_roots() function.
Let’s run the code and see what happens:
The code returns the IndentationError, and the tick indicates the line responsible for the error.
Solution
We can use a text editor like Sublime Text to see the indentation style in our code by highlighting it, as shown below.

Each line represents a tab, and a dot represents a space. We can see a mix of spaces and tabs in the code snippet, particularly the line sqrt_number = number ** 0.5 . To fix this, you can change replace the indentation on the other lines with four white spaces as this is the preferred indentation method. Alternatively, you can use tabs. Let’s look at the revised code in the text editor:

We can see each line has spaces instead of tabs. Let’s run the code to see what happens:
The program returns the square root of each number in the list we pass to the function. You do not need to use a text editor to find differences in indentation style, but it does make it easier to spot them. Alternatively, you can manually go through each line in your code and stick to one indentation style.
Summary
Congratulations on reading to the end of this tutorial! The error “indentationerror: unindent does not match any outer indentation level” occurs when you mix spaces and tabs to indent your code. To solve this error, ensure all your code uses either spaces or tabs in your program. You can use a text editor to highlight your code to make it easier to spot the differences. Otherwise, you can go over the lines in your code and stick to one indentation style. To minimise the likelihood of this error, you can write your code in a text editor or Integrated Development Environment (IDE) like PyCharm, which automatically indent your code in a consistent style.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
How to Fix: Python indentationerror: unindent does not match any outer indentation level Solution

In this tutorial, you’ll learn how to fix a common error you’ll encounter in Python, the indentation error. Indenting your code is an important function for writing useful programs. However, indenting code improperly will raise the following error: “Python indentationerror: unindent does not match any outer indentation level”.
By the end of this tutorial, you’ll have learned:
- How to fix the Python indentation error
- How indentation works in Python and why it matters
- How to use popular IDEs, such as VS Code, to easily resolve the Python indentation error
Let’s get started!
Table of Contents
What is the Python Indentation Error?
The Python indentationerror is caused by a number of different factors:
- Mixing spaces and tabs in indentation
- Mismatched levels of indentation in code blocks
Both of these scenarios will cause the indentationerror to be raised. Unfortunately, it’s not immediately clear which of these scenarios caused the error to happen. Before we dive into further details, let’s explore how indentation works in Python.
How Indentation Works in Python
Python allows you to indent your code with either spaces or tabs. It doesn’t matter which one you choose – however it requires you to be consistent in that choice. Because Python is a statically typed language, Python will not be able to work properly if spaces and tabs are both used for indentation.
In fact, when copying code from the internet, this is a common error you’ll encounter. Throughout the rest of this tutorial, you’ll learn how to identify and resolve this error.
Should You Indent with Spaces or Tabs in Python?
While it doesn’t matter whether you use tabs or spaces to indent your code, Python does provide some guidance as to which methods are preferred. The PEP-8 standard on indenting your code indicates that using four spaces per indentation level is the preferred method of indenting your code.
The PEP standard goes even further. It says that spaces are preferred and that tabs should only be used to remain consistent with existing code.
Causes of the Python Indentation Error
Let’s explore the causes of the Python indentationerror a little further. Earlier, you learned that the error is caused by mixing spaces and tabs in indenting code as well as having improperly indented code.
Let’s explore both of these in further detail in order to see how the error can be resolved.
Mixing Spaces and Tabs in Python
One of the most common causes of the Python indentationerror is mixing tabs and spaces in your code when indenting. The problem with this is that it’s often not intuitive to find. This problem mostly occurs when you copy and paste code from another source into a Python program.
Thankfully, most modern IDEs will account for this and automatically convert tabs into spaces. For example, Visual Studio Code will, by default, convert tabs into the corresponding number of spaces. Because of this, this error is more common when you’re running Python programs in other applications.
In many cases, you’ll be able to select tabs using find and replace by searching for the \t string.
Incorrect Indenting in Python
The other cause of the Python indentationerror is having incorrect indenting in your Python code. This can occur when you’re writing any blocked type pieces of code, such as if-else statements or for loops.
Let’s take a look at an example that would throw a Python indetationerror :
You can fix this easily by indenting the second like of code, as shown below:
Being mindful of where your code needs to be indented is an important step in writing error-free code. Generally, following a colon, your code will need to be indented.
Conclusion
In this tutorial, you learned how to solve the Python indentationerror. You learned what causes the Python indentationerror and how to solve its two main causes. First, we looked at how the error is thrown when your code mixes both spaces and tabs. Then, we looked at missing or erroneous indentations of your code.