需要帮助查找我的列表代码中的 indentation/syntax 错误

Need help finding an indentation/syntax error in my list code

好的,我正在通过 doctest 来检查我的所有代码。我还有大约五个其他工作正常的解决方案 - 所以我的代码的主要内容是功能性的。但是,我经常在一次测试中失败,并且得到的是 False 而不是 True。下面是我的代码

def duplist(lst1, lst2):
    lst1Counter = 0
    lst2Counter = 0
    while lst1Counter in range(len(lst1)) and lst2Counter in range(len(lst2)):
        if lst1[0] == lst2[0]:
            lst1Counter+=1
            lst2Counter+=1
        else:
            lst2Counter+=1

    if lst1Counter not in range(len(lst1)):
        return True
    else:
        return False

列表的重点是看第一个列表是否是第二个列表的子列表。除了一项测试外,一切都已准备就绪。

duplist([15,1,100],[20,15,30,50,1,100])==True

我总是return假的。这一定是一个小 syntax/indentation 错误 - 但我无法在任何地方找到它。有人能指出我正确的方向吗?

可视化您的代码正在做什么可能会为您指明正确的方向。 让我们回顾一下代码:

对于duplist([15, 1, 100], [20, 15, 30, 50, 1, 100]),while循环完成后,你剩下:

lst1Counter = 0
lst2Counter = 3  # for 3 iterations where lst1[0] != lst2[0] in the above case

那么你正在寻找是否 lst1Counter not in range(len(lst1)) 是:

if 0 not in [0, 1, 2]

这是一个错误的陈述,因为 0 在 [0, 1, 2] 中。 因此,它 returns False 而不是 True。

此外,如果您的代码需要一些帮助,请告诉我们,它实际上并没有检查 lst1 是否是 lst2 的子列表。

希望对您有所帮助!