When I compile the Python code below, I get
IndentationError: unindent does not match any outer indentation level
import sys def Factorial(n): # Return factorial result = 1 for i in range (1,n): result = result * i print "factorial is ",result return result
Why?
The Python "IndentationError: unindent does not match any outer indentation level" occurs when we mix tabs and spaces in the same code block. To solve the error, remove the spacing and only use tabs or spaces, but don't mix the two in the same code block.
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.
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.
IMPORTANT: Spaces are the preferred method - see PEP 8 Indentation and Tabs or Spaces?. (Thanks to @Siha for this.)
For Sublime Text users:
Set Sublime Text to use tabs for indentation: View --> Indentation --> Convert Indentation to Tabs
Uncheck the Indent Using Spaces option as well in the same sub-menu above. This will immediately resolve this issue.
Other posters are probably correct...there might be spaces mixed in with your tabs. Try doing a search & replace to replace all tabs with a few spaces.
Try this:
import sys def Factorial(n): # return factorial result = 1 for i in range (1,n): result = result * i print "factorial is ",result return result print Factorial(10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With