类型错误:'NoneType'对象不可迭代

22 投票
2 回答
205885 浏览
提问于 2025-04-18 05:03

我正在用Python制作一个猜字游戏。在这个游戏中,有一个Python文件里有一个函数,它会从一个数组中随机选择一个字符串,并把它存储在一个变量里。然后,这个变量会被传递给另一个文件中的一个函数。这个函数会把用户的猜测存储为一个字符串在一个变量中,然后检查这个猜测是否在所选的单词里。然而,每当我输入一个字母并按下回车键时,就会出现我问题标题中的错误。顺便说一下,我使用的是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("_")

接下来是接收用户猜测并判断它是否在游戏选定的单词中的代码(不用关注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"

到目前为止,我尝试过用str()把游戏中的“guess”变量转换成字符串,尝试过用.lower()把它变成小写,还在words文件中做过类似的操作。以下是我运行时得到的完整错误信息:

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:

你看到的“main.py”是我写的另一个Python文件。如果你想看其他文件,请告诉我。不过,我觉得我展示的这些是唯一重要的。谢谢你的时间!如果我遗漏了什么重要细节,请告诉我。

2 个回答

0

这个Python错误提示说 wordInput 不是一个可迭代的对象——它的类型是NoneType。

如果你在出错的那一行之前打印一下 wordInput,你会发现它的值是 None

因为 wordInputNone,这就意味着传给函数的参数也是 None。在这个例子中,就是 word。你把 pickEasy 的结果赋值给了 word

问题在于你的 pickEasy 函数没有返回任何东西。在Python中,如果一个方法没有返回任何值,它的默认返回值就是NoneType。

我想你是想返回一个 word,所以你可以这样做:

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

如果一个函数没有返回任何东西,比如:

def test():
    pass

那么它的隐含返回值就是 None

所以,你的 pick* 方法没有返回任何东西,比如:

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

调用它们的那几行代码,比如:

word = pickEasy()

会让 word 的值变成 None,因此在 getInput 中的 wordInput 也是 None。这就意味着:

if guess in wordInput:

等同于:

if guess in None:

NoneNoneType 的一个实例,它不支持迭代功能,所以你会遇到类型错误。

解决办法是添加返回值类型:

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

撰写回答