Python 缩进错误:未缩进与任何外部缩进级别不匹配

Python 缩进错误:未缩进与任何外部缩进级别不匹配,python,Python,这两条线 def LCS(word_list1, word_list2): m = len(word_list1) n = len(word_list2) print m print n C = [[0] * (n+1) for i in range(m+1)] # IndentationError: unindent does not match any outer indentation level print C

这两条线

def LCS(word_list1, word_list2):
    m = len(word_list1)
    n = len(word_list2)
    print m
    print n
    C = [[0] * (n+1) for i in range(m+1)]   # IndentationError: unindent does not match          any outer indentation level
    print C
    i=0
    j=0
    for word in word_list1:
        j=0
        for word in word_list2:
        if word_list1[i-1] == word_list2[j-1]: 
            C[i][j] = C[i-1][j-1] + 1
        else:
            C[i][j] = max(word_list1(C[i][j-1], C[i-1][j]))
        j+=1
    i+=1
    return C
应在同一级别缩进。即:

  C = [[0] * (n+1) for i in range(m+1)]   # IndentationError: unindent does not match          any outer indentation level
print C
更新

Op已纠正了上述问题。我检查了代码,现在错误在别处:

C = [[0] * (n+1) for i in range(m+1)]
print C
应该是:

for word in word_list2:
if word_list1[i-1] == word_list2[j-1]: 
    C[i][j] = C[i-1][j-1] + 1
else:
    C[i][j] = max(word_list1(C[i][j-1], C[i-1][j]))
j+=1

当问题不断被编辑并且缩进被纠正时,很难回答为什么缩进不正确的问题

但是,我建议您在编写更多Python代码和脚本之前先阅读。这可以解释为什么在更正缩进后,第七行仍然会出现
IndentationError


我还建议您尝试使用运行脚本,以确定何时意外混合了选项卡和空格。当然,任何一个像样的编辑器都能够突出显示制表符和空格(例如)。

我认为它应该使用混合缩进-制表符和空格

建议:每个缩进级别使用4个空格,
不要把标签和空格混在一起。 最大线路长度:80

检查Python编辑器的设置

GEdit: 从工具->首选项->编辑器->选项卡宽度=4,使用空格代替选项卡

日食: 使用Pydev-

维姆: 使用以下vim设置-适用于vim编辑器

for word in word_list2: 
    # These lines have been indented extra.
    if word_list1[i-1] == word_list2[j-1]: 
        C[i][j] = C[i-1][j-1] + 1
    else:
        C[i][j] = max(word_list1(C[i][j-1], C[i-1][j]))
    j+=1

哇,python解释器已经回答了您的问题“缩进错误:未缩进不匹配”。好的python IDE会在您键入代码时突出显示错误的缩进。我花了两分钟想知道注释行是如何缩进错误的。天哪,为什么有人纠正了问题中的错误?没有任何通知吗?@Aneeshia。它没有显示相同的错误。它显示了一个不同的错误,因为您有两个不同的错误开始。@Aneeshia:我无法重现您的错误。你为什么不确定你的缩进是一致的?在任何地方都使用空格或制表符,但不要混用。我在使用不同VIM设置的不同机器编辑文件时遇到了一个问题。“列表”帮助我了解到,原来的机器插入了制表符,但其他空格。在我的新机器上设置“noexpandtab”修复了它。将vim modeline添加到我的文件中帮助我确保它在任何地方都能正常工作。
:set tabstop=4 expandtab shiftwidth=4 softtabstop=4