当我编译下面的Python代码时,我得到
IndentationError: unindent不匹配任何外部缩进级别
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?
当我编译下面的Python代码时,我得到
IndentationError: unindent不匹配任何外部缩进级别
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?
当前回答
在原子
去
Packages > Whitespace > Convert Spaces to Tabs
然后再次检查文件缩进:
python -m tabnanny yourFile.py
or
>python
>>> help("yourFile.py")
其他回答
我犯了同样的错误,因为另一件事,不是关于制表符和空格。我有第一个比其他的稍微多一点缩进:更深入。如果它只是一两个空间,你可能会在一个很长的代码块之后检查它。文档字符串也是一样:
"""comment comment
comment
"""
它们还需要对齐,请参阅同一页上的另一个答案。
可复制的几行:
if a==1:
print('test')
else:
print('test2')
抛出:
File "<ipython-input-127-52bbac35ad7d>", line 3
else:
^
IndentationError: unindent does not match any outer indentation level
在原子
去
Packages > Whitespace > Convert Spaces to Tabs
然后再次检查文件缩进:
python -m tabnanny yourFile.py
or
>python
>>> help("yourFile.py")
对于Atom用户,Packages ->whitspace ->删除尾随空格 这对我很有效
因为我意识到没有特定于spyder的答案,我将添加一个: 基本上,仔细查看你的if语句,并确保所有if, elif和else都有相同的间距,即它们在开始时处于同一行,如下所述:
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!")
只是一个补充。我在notepad++中有一个类似的缩进问题。
Unexcepted缩进 外压痕水平 去---->搜索选项卡---->点击替换---->点击单选按钮扩展下面->现在用四个空格替换\t 去---->搜索选项卡---->点击替换---->点击单选按钮扩展下面->现在替换\n与无