簡體   English   中英

TypeError:“NoneType”類型的參數不可迭代

[英]TypeError: argument of type 'NoneType' is not iterable

我正在使用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"

到目前為止,我已經嘗試將gamePlay函數中的“guess”變量轉換為帶有str()的字符串,我嘗試使用.lower()將其設置為小寫,並且我在單詞文件中做了類似的事情。 這是我運行時遇到的完整錯誤:

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

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不是可迭代的 - >它是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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM