IndentationError: unindent does not match any outer indentation level

Total
0
Shares

Python throws the error, IndentationError: unindent does not match any outer indentation level, When you mix spaces and tabs in the same code. You can either use tabs or spaces but not both. Also, it is recommended to use spaces.

Consider this code example –

def SuperHero(ind):
	superList = ["Captain Americal", "Ironman", "Thor", "Hulk"];
    if(ind >=0 and ind < len(superList)):
        return superList[ind];
    else:
        return "Error: Index out of bounds";

print(SuperHero(2));

This code will throw the error that, “IndentationError: unindent does not match any outer indentation level” because in superList line, I have used tab for spacing while everywhere else I have used spaces. It’s not visible because I have used equal number of spaces as that of tab.

To solve this you need to remove the tab with spaces but for that you need to know where we have used tab in the code. Simple find and replace in text editors will help. Search for tab and replace with 4 spaces.

    Tweet this to help others