簡體   English   中英

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” ..這是行不通的。 我也嘗試使用

如果A_words中的word2player2和而不是usedlist中的word2player2:>>,但這也不起作用。

任何意見表示贊賞。

方法append添加元素“ inplace”,這意味着不會返回新列表,而是會更新從中調用該方法的原始列表。 因此,它不返回任何內容( None ),並且您會收到此錯誤。

正如其他評論所建議的那樣,而不是重新分配變量

usedlist=usedlist.append(word1player1)

只需應用append函數, usedlist將具有新的期望值:

usedlist.append(word1player1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM