Search code examples
pythonindentationpython-2.x

IndentationError: unindent does not match any outer indentation level, although the indentation looks correct


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?


Solution

  • One possible cause for this error is that there might be spaces mixed with tabs for indentation. 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)