当我编译下面的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?
当前回答
如果你使用notepad++,用扩展搜索模式做一个“替换”来找到\t并替换为四个空格。
其他回答
您确定在缩进空白中没有混合制表符和空格吗?(这会导致错误。)
注意,建议不要在Python代码中使用制表符。请参阅样式指南。您应该配置notepad++为制表符插入空格。
为了它的价值,我的文档字符串缩进太多,这也抛出相同的错误
class junk:
"""docstring is indented too much"""
def fun(): return
IndentationError: unindent不匹配任何外部缩进级别
实际上,我从一个错误的地方得到了这个。
我加上这个答案是因为我花了很多时间寻找标签。 在本例中,它与制表符或空格无关。
def some_instance_function(self):
json_response = self.some_other_function()
def compare_result(json_str, variable):
"""
Sub function for comparison
"""
json_value = self.json_response.get(json_str, f"{json_str} not found")
if str(json_value) != str(variable):
logging.error("Error message: %s, %s",
json_value,
variable) # <-- Putting the bracket here causes the error below
#) <-- Moving the bracket here fixes the issue
return False
return True
logging.debug("Response: %s", self.json_response)
# ^----The pylint error reports here
我得到了类似的错误如下:
IndentationError:期望一个缩进的块
当我忘记把pass传给下面的类或函数时,然后在类或函数之后编写其他代码,如下所示:
class Person:
# pass
x = 10
def test():
# pass
x = 10
并且,当其他代码没有在类或函数之后编写时,如下所示:
class Person:
# pass
# x = 10
def test():
# pass
# x = 10
我得到的错误如下:
SyntaxError:解析时意外的EOF
例如:
1. def convert_distance(miles):
2. km = miles * 1.6
3. return km
在这段代码中,我遇到了同样的情况。只需要删除之前的缩进空格 第2行和第3行,然后使用制表符或空格。不要两者都用。在python中编写代码时,给予适当的缩进。 对于Spyder去源代码>修复缩进。同样适用于VC代码和sublime文本或任何其他编辑器。固定缩进。