当我编译下面的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?
当前回答
如果你正在使用Vim,点击escape,然后输入
gg=G
这将自动缩进所有内容,并将清除您所扔进去的任何空间。
其他回答
如果你使用Python的IDLE编辑器,你可以按照它在类似错误消息中建议的那样做:
1)全选,如Ctrl + A
2)进入Format -> Untabify Region
3)再次检查缩进是否正确,保存并重新运行程序。
我使用的是Python 2.5.4
其他的海报可能是正确的…可能会在制表符中混入空格。尝试用一些空格替换所有的制表符。
试试这个:
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)
对于Atom用户,Packages ->whitspace ->删除尾随空格 这对我很有效
为了它的价值,我的文档字符串缩进太多,这也抛出相同的错误
class junk:
"""docstring is indented too much"""
def fun(): return
IndentationError: unindent不匹配任何外部缩进级别
这可能是因为上面的函数没有以同样的方式缩进。 即。
class a:
def blah:
print("Hello world")
def blah1:
print("Hello world")