当我编译下面的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?
当前回答
另一种纠正缩进错误的方法是复制您的 代码到PyCharm(如果您已经配置了),并重新格式化文件 它将自动正确地缩进。
其他回答
另一种纠正缩进错误的方法是复制您的 代码到PyCharm(如果您已经配置了),并重新格式化文件 它将自动正确地缩进。
使用Visual studio代码
如果你使用vs code than,它将使用下面的简单步骤将所有混合缩进转换为空格或制表符。
按Ctrl + Shift + p 使用空格输入缩进 按回车键
如果你正在使用Vim,点击escape,然后输入
gg=G
这将自动缩进所有内容,并将清除您所扔进去的任何空间。
我定义了一个函数,但它除了函数注释之外没有任何内容……
def foo(bar):
# Some awesome temporary comment.
# But there is actually nothing in the function!
# D'Oh!
它喊道:
File "foobar.py", line 69
^
IndentationError: expected an indented block
(注意^标记所指向的行是空的)
--
多个解决方案:
1:只注释掉函数
2:添加函数注释
def foo(bar):
'' Some awesome comment. This comment could be just one space.''
3:添加不做任何事情的行
def foo(bar):
0
在任何情况下,确保清楚地说明为什么它是一个空函数——对于你自己,或者对于将使用你的代码的同事
我昨天也有同样的问题,这是缩进错误,正在使用崇高的文本编辑器。我花了几个小时来修复它,最后我把代码复制到VI文本编辑器中,它工作得很好。Ps python对空格太敏感,请确保不要混合空格和制表符。