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

Python: not iterable error on trying to look up two lists

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

问题出在下面,我认为:

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" ..但这不起作用。我也尝试使用

在一行中完成

如果 word2player2 在 A_words 而 word2player2 不在 usedlist 中:>> 但那也不起作用。

任何意见表示赞赏。

方法 append 添加元素 "inplace",这意味着 return 不是一个新列表,而是更新该方法所在的原始列表叫。因此它 return 什么都没有 (None) 并且您收到此错误。

正如其他评论所建议的,而不是重新分配变量

usedlist=usedlist.append(word1player1)

只需应用追加函数,usedlist 就会得到新的期望值:

usedlist.append(word1player1)