Python TypeError:“NoneType”类型的参数不可迭代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23417403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:56:51  来源:igfitidea点击:

TypeError: argument of type 'NoneType' is not iterable

pythonpython-2.7

提问by user163505

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:

我正在用 Python 制作一个 Hangman 游戏。在游戏中,一个python文件有一个函数,它从数组中随机选择一个字符串并将其存储在一个变量中。然后将该变量传递给另一个文件中的函数。该函数将用户猜测作为字符串存储在变量中,然后检查该猜测是否在单词中。但是,每当我输入一个字母并按 Enter 键时,我都会收到此问题标题中的错误。请注意,我使用的是 Python 2.7。下面是带单词的函数的代码:

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.)):

现在这里是让用户猜测并确定它是否在为游戏选择的单词中的代码(不要注意 wordCount 变量。另外,“words”是上面代码的文件名。) ):

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:

到目前为止,我已经尝试使用 str() 将 gamePlay 函数中的“guess”变量转换为字符串,我尝试使用 .lower() 将其设置为小写,并且我在 word 文件中做了类似的事情。这是我运行时得到的完整错误:

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.

你看到的“main.py”是我写的另一个python文件。如果你想看其他人,请告诉我。但是,我觉得我展示的那些是唯一重要的。感谢您的时间!如果我遗漏了任何重要的细节,请告诉我。

采纳答案by reece

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

如果函数不返回任何内容,例如:

def test():
    pass

it has an implicit return value of None.

它有一个隐式返回值None

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

因此,由于您的pick*方法不返回任何内容,例如:

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 wordto None, so wordInputin getInputis None. This means that:

wordNone,所以wordInputgetInputNone。这意味着:

if guess in wordInput:

is the equivalent of:

相当于:

if guess in None:

and Noneis an instance of NoneTypewhich does not provide iterator/iteration functionality, so you get that type error.

andNone是一个NoneType不提供迭代器/迭代功能的实例,因此您会收到该类型错误。

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

回答by Bartlomiej Lewandowski

The python error says that wordInputis not an iterable -> it is of NoneType.

python 错误说这wordInput不是可迭代的 -> 它是 NoneType。

If you print wordInputbefore the offending line, you will see that wordInputis None.

如果wordInput在违规行之前打印,您将看到它wordInputNone.

Since wordInputis None, that means that the argument passed to the function is also None. In this case word. You assign the result of pickEasyto word.

由于wordInputis None,这意味着传递给函数的参数也是None。在这种情况下word。您将结果分配pickEasyword

The problem is that your pickEasyfunction does not return anything. In Python, a method that didn't return anything returns a NoneType.

问题是您的pickEasy函数不返回任何内容。在 Python 中,不返回任何内容的方法返回 NoneType。

I think you wanted to return a word, so this will suffice:

我想你想返回 a word,所以这就足够了:

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