IndentationError: unindent does not match any outer indentation level

后端 未结 30 1862
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:43

    Since I realize there's no answer specific to spyder,I'll add one: Basically, carefully look at your if statement and make sure all if, elif and else have the same spacing that is they're in the same line at the start like so:

    def your_choice(answer):
        if answer>5:
            print("You're overaged")
        elif answer<=5 and answer>1: 
                print("Welcome to the toddler's club!")
        else:
                print("No worries mate!")
    
    0 讨论(0)
  • 2020-11-21 08:43

    I had a function defined, but it did not had any content apart from its function comments...

    def foo(bar):
        # Some awesome temporary comment.
        # But there is actually nothing in the function!
        # D'Oh!
    

    It yelled :

      File "foobar.py", line 69
    
                                    ^
    IndentationError: expected an indented block
    

    (note that the line the ^ mark points to is empty)

    --

    Multiple solutions:

    1: Just comment out the function

    2: Add function comment

    def foo(bar):
        '' Some awesome comment. This comment could be just one space.''
    

    3: Add line that does nothing

    def foo(bar):
        0
    

    In any case, make sure to make it obvious why it is an empty function - for yourself, or for your peers that will use your code

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

    IMPORTANT: Spaces are the preferred method - see PEP008 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.

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

    I am using Sublime Text 3 with a Flask project. I fixed the error using View > Indentation > Tab Width: 4 after unselected Indent Using Spaces

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

    For what its worth, my docstring was indented too much and this also throws the same error

    class junk: 
         """docstring is indented too much""" 
        def fun(): return   
    

    IndentationError: unindent does not match any outer indentation level

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

    I had the same issue yesterday, it was indentation error, was using sublime text editor. took my hours trying to fix it and at the end I ended up copying the code into VI text editor and it just worked fine. ps python is too whitespace sensitive, make sure not to mix space and tab.

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