【问题标题】:IndentationError: unindent does not match any outer indentation level pythonIndentationError: unindent 不匹配任何外部缩进级别python
【发布时间】:2016-01-27 11:46:42
【问题描述】:

你好,这是我的 python 代码:

def is_palindrome(nombre):
    if str(nombre) == str(nombre)[::-1]:
        return True
    return False
x = 0
for i in range(100, 1000):
    for j in range(i, 1000):
        for z in range(j, 1000):    
 produit = i*j*z

        if is_palindrome(produit):
            if produit > x:
                x = produit
print(x)

当我试图编译这段代码时,我得到了这个错误: 产品 = ijz ^ IndentationError: unindent 不匹配任何外部缩进级别 请问有人有想法吗??

【问题讨论】:

  • 我知道这个问题:unindent 不匹配任何外部缩进级别。缩进在 Python 中很重要,所以你必须修复它。检查每个级别使用了多少空间,检查是否有标签,等等。
  • 事实上:程序正在运行,但是当我在 range(j, 1000) 中添加 z 时:我将 i 和 j 乘以 z 我得到了错误
  • 我不明白为什么
  • 因为您添加的代码使用了不匹配的缩进级别。如果不能保证您发布的代码完全匹配程序中的缩进,我只能这么说。
  • 现在看来,produit = i*j*z 行的缩进显然是错误的

标签: python python-2.7 python-3.x wxpython ipython


【解决方案1】:

您也可以在命令行中检查制表符/空格的问题:

python -m tabnanny -v yourfile.py

【讨论】:

    【解决方案2】:

    您问题中的代码一团糟。这是固定缩进后的样子。

    def is_palindrome(nombre):
        if str(nombre) == str(nombre)[::-1]:
            return True
        return False
    
    x = 0
    for i in range(100, 1000):
        for j in range(i, 1000):
            for z in range(j, 1000):    
                produit = i*j*z
    
                if is_palindrome(produit):
                    if produit > x:
                        x = produit
    # you may want to move this into the inner `if` statement
    print(x)
    

    【讨论】:

      猜你喜欢
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多