Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: argument of type 'NoneType' is not iterable

I am making a Hangman game in Python. In the game, one python file has a function that selects a random string from an array and stores it in a variable. That variable is then passed to a function in another file. That function stores a users guess as a string in a variable, then checks to see if that guess is in the word. However, whenever I type a letter and press enter, I get the error in the title of this question. Just so you know, I'm using Python 2.7. Here's the code for the function that takes a word:

import random

easyWords = ["car", "dog", "apple", "door", "drum"]

mediumWords = ["airplane", "monkey", "bananana", "window", "guitar"]

hardWords = ["motorcycle", "chuckwalla", "strawberry", "insulation", "didgeridoo"]

wordCount = []

#is called if the player chooses an easy game. 
#The words in the array it chooses are the shortest.
#the following three functions are the ones that
#choose the word randomly from their respective arrays.
def pickEasy():
    word = random.choice(easyWords)
    word = str(word)
    for i in range(1, len(word) + 1):
        wordCount.append("_")

#is called when the player chooses a medium game.
def pickMedium():
    word = random.choice(mediumWords)
    for i in range(1, len(word) + 1):
        wordCount.append("_")

#is called when the player chooses a hard game. 
def pickHard():
    word = random.choice(hardWords)
    for i in range(1, len(word) + 1):
        wordCount.append("_")

Now here is the code that takes the users guess and determines whether or not it is in the word chosen for the game (Pay no attention to the wordCount variable. Also, "words" is the name of the file with the code above.)):

from words import *
from art import *

def gamePlay(difficulty):
    if difficulty == 1:
        word = pickEasy()
        print start
        print wordCount
        getInput(word)

    elif difficulty == 2:
        word = pickMedium()
        print start
        print wordCount

    elif difficulty == 3:
        word = pickHard()
        print start
        print wordCount

def getInput(wordInput):
    wrong = 0
    guess = raw_input("Type a letter to see if it is in the word: \n").lower()

    if guess in wordInput:
        print "letter is in word"

    else:
        print "letter is not in word"

So far I have tried converting the "guess" variable in the gamePlay function to a string with str(), I've tried making it lowercase with .lower(), and I've done similar things in the words file. Here is the full error I get when I run this:

File "main.py", line 42, in <module>
    main()
  File "main.py", line 32, in main
    diff()
  File "main.py", line 17, in diff
    gamePlay(difficulty)
  File "/Users/Nathan/Desktop/Hangman/gameplay.py", line 9, in gamePlay
    getInput(word)
  File "/Users/Nathan/Desktop/Hangman/gameplay.py", line 25, in getInput
    if guess in wordInput:

The "main.py" you see is another python file I wrote. If you would like to see the others let me know. However, I feel the ones I've shown are the only important ones. Thank you for your time! Let me know if I left out any important details.

like image 692
user163505 Avatar asked May 01 '14 22:05

user163505


People also ask

How do I fix NoneType object is not iterable in Python?

The Python "TypeError: 'NoneType' object is not iterable" occurs when we try to iterate over a None value. To solve the error, figure out where the variable got assigned a None value and correct the assignment or check if the variable doesn't store None before iterating.

What is NoneType object is not iterable?

The TypeError: 'NoneType' object is not iterable error is raised when you try to iterate over an object whose value is equal to None. To solve this error, make sure that any values that you try to iterate over have been assigned an iterable object, like a string or a list.

How do you fix an object not iterable?

How to Fix Int Object is Not Iterable. One way to fix it is to pass the variable into the range() function. In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.

How does Python handle NoneType?

Technically, you can prevent the NoneType exception by checking if a value is equal to None using is operator or == operator before you iterate over that value. To solve the NoneType error, ensure that any values you try to iterate over should be assigned an iterable object, like a string or a list.


1 Answers

If a function does not return anything, e.g.:

def test():
    pass

it has an implicit return value of None.

Thus, as your pick* methods do not return anything, e.g.:

def pickEasy():
    word = random.choice(easyWords)
    word = str(word)
    for i in range(1, len(word) + 1):
        wordCount.append("_")

the lines that call them, e.g.:

word = pickEasy()

set word to None, so wordInput in getInput is None. This means that:

if guess in wordInput:

is the equivalent of:

if guess in None:

and None is an instance of NoneType which does not provide iterator/iteration functionality, so you get that type error.

The fix is to add the return type:

def pickEasy():
    word = random.choice(easyWords)
    word = str(word)
    for i in range(1, len(word) + 1):
        wordCount.append("_")
    return word
like image 95
reece Avatar answered Oct 04 '22 04:10

reece