简体   繁体   中英

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

I have the following code which seeks to continue only if the variable is IN the first list and NOT in the second list.

The problem is in the below, I think:

if word2player2 in A_words:
            if word2player2 not in usedlist:

Whole Python code (for the function that is relevant)

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()

The error message is:

  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

I am using "in" and "not in" ..and that doesn't work. I also tried doing it on one line using

if word2player2 in A_words and word2player2 not in usedlist: >> but that didn't work either.

Any comments appreciated.

The method append adds the elements "inplace", which means that doesn't return a new list, instead of that, it updates the original list from which the method is called. Hence it returns nothing ( None ) and you receive this error.

As other comments suggested, instead of reassigning the variable

usedlist=usedlist.append(word1player1)

just apply the append function and usedlist will have the new desired value:

usedlist.append(word1player1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM