当我编译下面的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?
当前回答
我得到了类似的错误如下:
IndentationError:期望一个缩进的块
当我忘记把pass传给下面的类或函数时,然后在类或函数之后编写其他代码,如下所示:
class Person:
# pass
x = 10
def test():
# pass
x = 10
并且,当其他代码没有在类或函数之后编写时,如下所示:
class Person:
# pass
# x = 10
def test():
# pass
# x = 10
我得到的错误如下:
SyntaxError:解析时意外的EOF
其他回答
其他的海报可能是正确的…可能会在制表符中混入空格。尝试用一些空格替换所有的制表符。
试试这个:
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)
因为我意识到没有特定于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!")
如果你正在使用Vim,点击escape,然后输入
gg=G
这将自动缩进所有内容,并将清除您所扔进去的任何空间。
我在Ubuntu操作系统中使用Sublime文本。要修复此问题,请转到
>缩进->将缩进转换为制表符
看起来是压痕问题。你不必在Python中匹配花括号,但你必须匹配缩进级别。
防止空格/制表符问题的最好方法是在文本编辑器中显示不可见的字符。这将为您提供一种快速防止和/或解决缩进相关错误的方法。
此外,注入复制粘贴的代码是这类问题的常见来源。