Python:在尝试查找两个列表时出现不可编辑的错误

2024-09-28 01:32:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,它只在变量在第一个列表中而不在第二个列表中时才继续。你知道吗

我认为,问题在于:

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在单词列表中,而word2player2不在使用列表中:>;>;但这也不起作用。你知道吗

任何意见,谢谢。你知道吗


Tags: 代码in列表playyourifisnot
1条回答
网友
1楼 · 发布于 2024-09-28 01:32:22

方法append添加元素“inplace”,这意味着它不会返回一个新列表,而是更新从中调用方法的原始列表。因此它不返回任何内容(None),您将收到此错误。你知道吗

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

usedlist=usedlist.append(word1player1)

只要应用append函数,usedlist就会有新的所需值:

usedlist.append(word1player1)

相关问题 更多 >

    热门问题