刽子手游戏中的输出错误

时间:2016-09-22 18:46:38

标签: python python-3.x

我需要写一些构成刽子手游戏的函数。我有代码,但我一直收到错误,我的代码没有完全运行,所以我甚至不知道我的代码是否正确。这是我的代码:

def isWordGuessed(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: boolean, True if all the letters of secretWord are in lettersGuessed;
      False otherwise
    '''
    if lettersGuessed == []:
        return False
    else:
        for i in secretWord:
            if i not in lettersGuessed:
                return False
        else:
            if i == secretWord[-1]:
                return True

def getGuessedWord(secretWord, lettersGuessed):
    lst= ''
    for e in secretWord:
        if e in lettersGuessed:
            lst += e 
        else:
            lst += '_'
    return lst

def getAvailableLetters(lettersGuessed):
    '''
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters that represents what letters have not
      yet been guessed.
    '''
    Alletters = string.ascii_lowercase
    result = list(Alletters)
    for i in lettersGuessed:
        if i in result:
            result.remove(i)
    transtring = ''.join(result)
    return transtring

def hangman(secretWord):
    '''
    secretWord: string, the secret word to guess.

    Starts up an interactive game of Hangman.

    * At the start of the game, let the user know how many 
      letters the secretWord contains.

    * Ask the user to supply one guess (i.e. letter) per round.

    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computers word.

    * After each round, you should also display to the user the 
      partially guessed word so far, as well as letters that the 
      user has not yet guessed.

    Follows the other limitations detailed in the problem write-up.
    '''
    print("Welcome to the Hangman game!")
    print("I am thinking of a word that is " + str(len(secretWord)) + " letters!")
    guesses = 8      
    lettersGuessed = []   
    Alletters = string.ascii_lowercase    
    while guesses > 0:    
        print("You have " + str(guesses) + " guesses left")
        print("Available letters: " + str(Alletters))
        letters = input("Please guess a letter: ")
        if type(letters) != str:
            print("Invalid input! please enter one letter!")
        else:
            letterslower = letters.lower()     
            lettersGuessed = lettersGuessed.append(letterslower) 
            if letterslower not in Alletters:
                print("Opps! you have already guessed that letter: " + getGuessedWord(secretWord, lettersGuessed))
            else:
                if isWordGuessed(secretWord, lettersGuessed) == "True":
                    print("Congradualations, you won!")
                else:
                    print("Good guess: " + getGuessedWord(secretWord, lettersGuessed))
                    guesses -= 1
                    Alletters = getAvailableLetters(lettersGuessed)
    print("You have ran out of guess, the word is " + str(secretWord))

这是输出(错误是黄色):

Welcome to the Hangman game!
I am thinking of a word that is 1 letters!
You have 8 guesses left
Available letters: abcdefghijklmnopqrstuvwxyz
Please guess a letter: c
Traceback (most recent call last):

File "submission.py", line 81, in hangman
  if isWordGuessed(secretWord, lettersGuessed) == "True":
File "simple_grade_hangman.py", line 107, in isWordGuessed
  if letter not in lettersGuessed:
TypeError: argument of type 'NoneType' is not iterable

这里发生了什么?

1 个答案:

答案 0 :(得分:2)

您的错误来自这一行:

lettersGuessed = lettersGuessed.append(letterslower)

这会修改列表就地并返回None。您在假设它返回对修改后的列表的引用的情况下进行操作,但它并没有。

因此,当您将其作为参数传递给函数isWordGuessed并执行if i not in lettersGuessed时,将会引发错误,因为lettersGuessedNone且{{1} }不能用作None表达式的目标。

只需使用in修改并保留更改:

append

此外,将布尔值(lettersGuessed.append(letterslower) )与Truestr)进行比较会得到错误的结果:

"True"

相反,由于>>> True == 'True' False 返回布尔值,请直接在isWordGuessed子句中使用它的返回值:

if