类型错误:“NoneType”类型的参数不是iterab

2024-05-13 23:55:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python做一个刽子手游戏。在游戏中,一个python文件有一个函数,该函数从数组中选择一个随机字符串并将其存储在一个变量中。然后将该变量传递给另一个文件中的函数。该函数将用户猜测存储为变量中的字符串,然后检查猜测是否在单词中。但是,每当我键入一个字母并按enter键时,我就会得到这个问题标题中的错误。你知道,我用的是Python2.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文件。如果你想见其他人,请告诉我。不过,我觉得我展示的是唯一重要的。谢谢你抽出时间!如果我遗漏了任何重要的细节,请告诉我。


Tags: 文件the函数inpyifismain
2条回答

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

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中的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错误指出wordInput不是iterable->;它是NoneType。

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

由于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

相关问题 更多 >