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?
回答 0
其他海报可能是正确的…选项卡中可能混有空格。尝试执行搜索和替换操作,以将所有标签替换为几个空格。
尝试这个:
import sysdefFactorial(n):# return factorial
result =1for i in range (1,n):
result = result * iprint"factorial is ",resultreturn resultprintFactorial(10)
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)
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.
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.
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.
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!")
回答 18
我定义了一个函数,但除了函数注释外,它没有任何内容。
def foo(bar):# Some awesome temporary comment.# But there is actually nothing in the function!# D'Oh!
大喊:
File"foobar.py", line 69^IndentationError: expected an indented block
(请注意,^标记指向的行为空)
–
多种解决方案:
1:仅将功能注释掉
2:添加功能注释
def foo(bar):''Some awesome comment.This comment could be just one space.''
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.
回答 20
对于它的价值,我的文档字符串缩进太多,这也引发了相同的错误
class junk:"""docstring is indented too much"""def fun():return
IndentationError: unindent does not match any outer indentation level
1. def convert_distance(miles):
2. km = miles * 1.6
3. return km
In this code same situation occurred for me. Just delete the previous indent spaces of
line 2 and 3, and then either use tab or space. Never use both. Give proper indentation while writing code in python.
For Spyder goto Source > Fix Indentation. Same goes to VC Code and sublime text or any other editor. Fix the indentation.
This happens mainly because of editor .Try changing tabs to spaces(4).the best python friendly IDE or Editors are pycharm ,sublime ,vim for linux.
even i too had encountered the same issue , later i found that there is a encoding issue .i suggest u too change ur editor.