python - 类型错误 : argument of type 'NoneType' is not iterable

标签 python python-2.7

我正在用 Python 制作 Hangman 游戏。在游戏中,一个 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() 将 gamePlay 函数中的“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文件。如果你想看看其他人,请告诉我。但是,我觉得我展示的那些是唯一重要的。感谢您的时间!如果我遗漏了任何重要的细节,请告诉我。

最佳答案

如果一个函数不返回任何东西,例如:

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()

设置wordNone,所以getInput中的wordInputNone。这意味着:

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

关于python - 类型错误 : argument of type 'NoneType' is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23417403/

相关文章:

python - 使用 Beautiful Soup 进行 HTML 抓取 - 不需要的换行符

python - python中两个时间戳的平均值

python - Django Admin 中的嵌套内联

python - "NameError: name ' sqlalchemy中的 float ' is not defined"

python - Pycharm:将文件夹标记为 'sources root' 对于子文件夹不是递归的

python - 使用xpath获取图像

python - time.sleep(x) 没有正常工作?

python - 从字典列表中获取一个键值匹配的常用字典列表

python - 将字典存储在文件中供以后检索

Python处理反向查找