TypeError:“NoneType"类型的参数不可迭代 [英] TypeError: argument of type 'NoneType' is not iterable

查看:115
本文介绍了TypeError:“NoneType"类型的参数不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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"是包含代码的文件名以上.)):

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

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 文件中做了类似的事情.这是我运行时得到的完整错误:

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:

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文件.如果你想看其他人,请告诉我.但是,我觉得我展示的那些是唯一重要的.感谢您的时间!如果我遗漏了任何重要细节,请告诉我.

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.

推荐答案

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

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

def test():
    pass

它有一个隐式返回值 None.

it has an implicit return value of None.

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

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

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

调用它们的行,例如:

word = pickEasy()

设置wordNone,所以getInput中的wordInput就是None.这意味着:

set word to None, so wordInput in getInput is None. This means that:

if guess in wordInput:

相当于:

if guess in None:

NoneNoneType 的一个实例,它不提供迭代器/迭代功能,所以你会得到那个类型错误.

and None is an instance of NoneType which does not provide iterator/iteration functionality, so you get that type error.

修复方法是添加返回类型:

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

这篇关于TypeError:“NoneType"类型的参数不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆