当我编译下面的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?
当前回答
为了方便地检查制表符/空格的问题,你可以这样做:
python -m tabnanny yourfile.py
当然,你也可以设置正确的编辑器:-)
其他回答
使用Visual studio代码
如果你使用vs code than,它将使用下面的简单步骤将所有混合缩进转换为空格或制表符。
按Ctrl + Shift + p 使用空格输入缩进 按回车键
这可能是因为上面的函数没有以同样的方式缩进。 即。
class a:
def blah:
print("Hello world")
def blah1:
print("Hello world")
我正在使用Sublime Text 3与Flask项目。我修复了使用视图>缩进>选项卡宽度:4后未选择缩进使用空间的错误
我定义了一个函数,但它除了函数注释之外没有任何内容……
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
在任何情况下,确保清楚地说明为什么它是一个空函数——对于你自己,或者对于将使用你的代码的同事
如果使用colab,则可以通过此命令避免错误。
< Ctrl-A > < Tab > < Shift-Tab >
它是所有的[制表符]缩进转换为[空格]缩进。然后点击OK。