当我编译下面的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?
当前回答
因为我意识到没有特定于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!")
其他回答
您确定在缩进空白中没有混合制表符和空格吗?(这会导致错误。)
注意,建议不要在Python代码中使用制表符。请参阅样式指南。您应该配置notepad++为制表符插入空格。
另一种纠正缩进错误的方法是复制您的 代码到PyCharm(如果您已经配置了),并重新格式化文件 它将自动正确地缩进。
使用Visual studio代码
如果你使用vs code than,它将使用下面的简单步骤将所有混合缩进转换为空格或制表符。
按Ctrl + Shift + p 使用空格输入缩进 按回车键
为了方便地检查制表符/空格的问题,你可以这样做:
python -m tabnanny yourfile.py
当然,你也可以设置正确的编辑器:-)
为了它的价值,我的文档字符串缩进太多,这也抛出相同的错误
class junk:
"""docstring is indented too much"""
def fun(): return
IndentationError: unindent不匹配任何外部缩进级别