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.
This question is related to
python
python-2.7
The python error says that wordInput
is not an iterable -> it is of NoneType.
If you print wordInput
before the offending line, you will see that wordInput
is None
.
Since wordInput
is None
, that means that the argument passed to the function is also None
. In this case word
. You assign the result of pickEasy
to word
.
The problem is that your pickEasy
function does not return anything. In Python, a method that didn't return anything returns a NoneType.
I think you wanted to return a word
, so this will suffice:
def pickEasy():
word = random.choice(easyWords)
word = str(word)
for i in range(1, len(word) + 1):
wordCount.append("_")
return word
Source: Stackoverflow.com