DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

Python indentationerror: unindent does not match any outer indentation level Solution

ItsMyCode |

Indentation in Python is important, and it makes your code well structured and clean. Python uses indentation to define code blocks. You can use either tabs or spaces to indent the code. However, using a combination of space and tab will result in indentationerror: unindent does not match any outer indentation level.

What is Indentation in Python?

In Python, indentation is done using whitespace. In simple terms, indentation refers to adding white space before a statement. According to the PEP 8 rule, the standard way to indent the code is to use 4 spaces per indent level.

Without indentation Python will not know which code to execute next or which statement belongs to which block and will lead to IndentationError.

Tabs or Spaces?

The best practice is to use spaces as the indentation, and the same is the preferred indentation by the PEP 8 rule.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

Mixing tabs and spaces is not allowed, and if you do that, Python will throw *indentationerror: unindent does not match any outer indentation level, * and the compilation of the code will fail.

Fix indentationerror: unindent does not match any outer indentation level

Let’s take few examples and find out the possible cause and solution for indentationerros in Python.

A mix of Spaces and Tabs

This would be a common scenario where the developers tend to make mistakes by mixing both spaces and tabs. Follow one approach consistently, either tab or space, to resolve the error but never use a mix of both.

*Example *

a=5
b=10

if a<b:
    print("Using 4 space for indentation")
    print("using tab for indentation")

Enter fullscreen mode Exit fullscreen mode

Output

  File "c:\Projects\Tryouts\listindexerror.py", line 6
    print("using tab for indentation")
IndentationError: unexpected indent
Enter fullscreen mode Exit fullscreen mode

Suppose you are using code editors like VS Code and Pycharm. In that case, it will automatically resolve the issue by converting from tabs to spaces or spaces to tab, depending on the IDE configuration settings. However, if you are using any other editor like notepad++ or sublime or using the command line for writing code, you may face this issue often, and the solution is to use one consistent approach.

Mismatch of Indent size inside a code block

If you are using any statements, loops, and functions, the code block inside should have the same indentation level. Otherwise, you wil get an IndentationError.

Example

number=6
for i in range(1,number):
    print (i)
        print(number)
Enter fullscreen mode Exit fullscreen mode

Output

 File "c:\Projects\Tryouts\listindexerror.py", line 4
    print(number)
IndentationError: unexpected indent
Enter fullscreen mode Exit fullscreen mode

Solution

number=6
for i in range(1,number):
    print (i)
    print(number)
Enter fullscreen mode Exit fullscreen mode

Wrong indentation or mismatch of a code block

Often in larger projects, the number of lines will be more, leading to a mismatch of code blocks while writing loops, statements, and functions.

A typical use case is an if-else statement where due to a large no of lines the, if block and else block indentation may differ, which leads to indentationerror: unindent does not match any outer indentation level.

Example

a=5
b=6

if a< b:
        print("a is smaller")
    else:
        print("b is smaller")
Enter fullscreen mode Exit fullscreen mode

Output

  File "c:\Projects\Tryouts\listindexerror.py", line 6
    else:
         ^
IndentationError: unindent does not match any outer indentation level
Enter fullscreen mode Exit fullscreen mode

Solution

a=5
b=6

if a< b:
        print("a is smaller")
else:
        print("b is smaller")
Enter fullscreen mode Exit fullscreen mode

Output

a is smaller
Enter fullscreen mode Exit fullscreen mode

The post Python indentationerror: unindent does not match any outer indentation level Solution appeared first on ItsMyCode.

Top comments (0)