Python:尝试查找两个列表时不会出现可迭代错误

问题描述 投票:-1回答:1

我有以下代码,只有当变量在第一个列表中而不在第二个列表中时才会继续。

问题在于下面,我认为:

if word2player2 in A_words:
            if word2player2 not in usedlist:

整个Python代码(用于相关的功能)

def play():
    print("====PLAY===")
    score=0
    usedlist=[]
    A_words=["Atrocious","Apple","Appleseed","Actually","Append","Annual"]
    word1player1=input("Player 1: Enter a word:")
    usedlist=usedlist.append(word1player1)
    print(usedlist)
    if word1player1 in A_words:
        score=score+1
        print("Found, and your score is",score)
    else:
        print("Sorry, not found and your score is",score)

    word2player2=input("Player 2: Enter a word:")
    if word2player2 in A_words:
        if word2player2 not in usedlist:
            usedlist=usedlist.append(word2player2)
            print("Found")
    else:
            print("Sorry your word doesn't exist or has been banked")
            play()

错误消息是:

  File "N:/Project 6/Mini_Project_6_Solution2.py", line 67, in play
    if word2player2 not in usedlist:
TypeError: argument of type 'NoneType' is not iterable

我正在使用“in”和“not in”..但这不起作用。我也试过在一行上使用它

如果A_words中的word2player2和word2player2不在usedlist中:>>但这也不起作用。

任何评论赞赏。

python list search iterable
1个回答
1
投票

方法append添加元素“inplace”,这意味着它不返回新的列表,而是更新,它更新调用该方法的原始列表。因此它什么都不返回(None),你收到这个错误。

正如其他评论所暗示的那样,而不是重新分配变量

usedlist=usedlist.append(word1player1)

只需应用追加函数,usedlist将具有新的期望值:

usedlist.append(word1player1)
© www.soinside.com 2019 - 2024. All rights reserved.