当我编译下面的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?
当前回答
重要的是: 空格是首选方法-请参阅PEP 8缩进和制表符或空格?(感谢@Siha。)
Sublime Text用户:
设置Sublime Text使用制表符缩进: 将缩进转换为制表符
在上面的同一子菜单中取消使用空格缩进选项。 这将立即解决这个问题。
其他回答
这一行:result = result * i应该缩进(它是for循环的主体)。
或者-你混合了空格和制表符
每当我遇到这个错误时,都是因为我在编辑器中混淆了制表符和空格。
因为我意识到没有特定于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!")
在原子
去
Packages > Whitespace > Convert Spaces to Tabs
然后再次检查文件缩进:
python -m tabnanny yourFile.py
or
>python
>>> help("yourFile.py")
重要的是: 空格是首选方法-请参阅PEP 8缩进和制表符或空格?(感谢@Siha。)
Sublime Text用户:
设置Sublime Text使用制表符缩进: 将缩进转换为制表符
在上面的同一子菜单中取消使用空格缩进选项。 这将立即解决这个问题。