IndentationError: unindent does not match any outer indentation level

后端 未结 30 1865
Happy的楠姐
Happy的楠姐 2020-11-21 07:48

When I compile the Python code below, I get

IndentationError: unindent does not match any outer indentation level




        
相关标签:
30条回答
  • 2020-11-21 08:38

    Looks to be an indentation problem. You don't have to match curly brackets in Python but you do have to match indentation levels.

    The best way to prevent space/tab problems is to display invisible characters within your text editor. This will give you a quick way to prevent and/or resolve indentation-related errors.

    Also, injecting copy-pasted code is a common source for this type of problem.

    0 讨论(0)
  • 2020-11-21 08:38

    If you use notepad++, do a "replace" with extended search mode to find \t and replace with four spaces.

    0 讨论(0)
  • 2020-11-21 08:38

    If you are using Sublime text for python development,you can avoid the error by using the package Anaconda.After installing Anaconda,

    1. open your file in sublime
    2. right click on the open spaces
    3. choose anaconda
    4. click on autoformat
    5. DONE Or Press CTRL+ALT+R.
    0 讨论(0)
  • 2020-11-21 08:40

    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)
    
    0 讨论(0)
  • 2020-11-21 08:42

    To easily check for problems with tabs/spaces you can actually do this:

    python -m tabnanny yourfile.py
    

    or you can just set up your editor correctly of course :-)

    0 讨论(0)
  • 2020-11-21 08:43

    If you use Python's IDLE editor you can do as it suggests in one of similar error messages:

    1) select all, e.g. Ctrl + A

    2) Go to Format -> Untabify Region

    3) Double check your indenting is still correct, save and rerun your program.

    I'm using Python 2.5.4

    0 讨论(0)
提交回复
热议问题