. Advertisement .
..3..
. Advertisement .
..4..
I encountered the following problem in completing my work:
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:
Below is the code I ran:
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("_")
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"
What’s causing it, and how can it be resolved in the “typeerror argument of type nonetype is not iterable“ in the python?
The cause: If a function returns nothing, for example:
It has a return value of None implicitly.
As your
pick*
methods don’t return anything, for example:the lines that call them, for example:
word
has been set toNone
, sowordInput
ingetInput
is alsoNone
. That is to say:has the same meaning as
Because
None
is an instance ofNoneType
, which lacks iterator/iteration capabilities, the type error occurs.The solution: You can fix this error by adding the return type:
The python error
wordInput
sayswordInput
is an iterable. It is actually of NoneType.You will notice
wordInput
if you printwordInput
prior to the line in question.wordInput
isNone
. This means that the argument passed on to the function is alsoNone
. This isword
. ThepickEasy
result is assigned toword
.Your
pickEasy
function doesn’t return anything. A Python method that doesn’t return any results in a NoneType.You may have wanted to return
word
. This will suffice.