Solve IndentationError: unindent does not match any outer indentation level

Majority of the time new Python developers face one common problem and that is of IndentationError: unindent does not match any outer indentation level and they are not able to figure out why this error occurred.

In this post, I’ll be discussing some of the ways to overcome IndentationError. But before getting started, I’d like to tell you guys that Python is very strict about Indentation and if any of your code blocks is not indented, the interpreter is going to complain about the indentation.

Let us see this with an example.

Here, you can see that the code has an extra space in front of it and when you’ll try to run your python file, this will throw an error.

How to Solve the Error?

Stop Using spaces

The best way to avoid these kinds of error is to stop using spaces for indentation and start using tabs for indentation.

When you mix spaces with tabs in your code, then the compiler throws an indentation error.

Use of Tabs

Use tabs in place of spaces and this will remove all the indentation error your python code may have. This maintains consistency which reduces the chance of indentation error in your code. Using spaces may lead to some extra space but tabs are of fixed width. It will always give you either 2 space or 4 space depending upon your editor’s configuration.

Using Code Formatter

One another way to get rid of indentation error is to use some code formatter.  This will help you to format your code properly and will help you to format code blocks which are not properly formatted.

I hope this will solve the error. Still if you have any queries ask in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *