IndentationError: unindent does not match any outer indentation level

后端 未结 30 1861
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:22

    Are you sure you are not mixing tabs and spaces in your indentation white space? (That will cause that error.)

    Note, it is recommended that you don't use tabs in Python code. See the style guide. You should configure Notepad++ to insert spaces for tabs.

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

    I was using Jupyter notebook and tried almost all of the above solutions (adapting to my scenario) to no use. I then went line by line, deleted all spaces for each line and replaced with tab. That solved the issue.

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

    Firstly, just to remind you there is a logical error you better keep result=1 or else your output will be result=0 even after the loop runs.

    Secondly you can write it like this:

    import sys
    
    def Factorial(n): # Return factorial
      result = 0
      for i in range (1,n):
         result = result * i
    
      print "factorial is ",result
      return result
    

    Leaving a line will tell the python shell that the FOR statements have ended. If you have experience using the python shell then you can understand why we have to leave a line.

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

    This is because there is a mix-up of both tabs and spaces. You can either remove all the spaces and replace them with tabs.

    Or, Try writing this:

    #!/usr/bin/python -tt
    

    at the beginning of the code. This line resolves any differences between tabs and spaces.

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

    In intellij with python plugin, Ctrl + Shift + Alt to reformat the document fixed the indent/tab/spaces problem

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

    For Spyder users goto Source > Fix Indentation to fix the issue immediately

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