'Hangman'游戏python代码测试

时间:2014-10-28 09:55:15

标签: python loops

我已经为Hangman游戏编写了这段代码,其中对手是计算机。但我不断得到我不知道如何解决的错误。请看看我。例如,我当前的错误是:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\Python code\hangmangame_test.py", line 112, in <module>
    hangman(a)
  File "C:\Users\User\Desktop\Python code\hangmangame_test.py", line 91, in hangman
    if isWordGuessed(secretWord, lettersGuessed) == "True":
  File "C:\Users\User\Desktop\Python code\hangmangame_test.py", line 12, in isWordGuessed
    if i in lettersGuessed:
TypeError: argument of type 'NoneType' is not iterable

import string
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
    '''
    # FILL IN YOUR CODE HERE...
    new = ""
    for i in secretWord:
        if i in lettersGuessed:
            new += i
            if new == secretWord:
                return True
        else:
            return False

def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    # FILL IN YOUR CODE HERE...
    result = list(secretWord)
    for i in result:
        if i not in lettersGuessed:
            result[result.index(i)] = " _ "
    transtring = ''.join(result)
    return transtring


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.
    '''
    # FILL IN YOUR CODE HERE...
    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.
    '''
    # FILL IN YOUR CODE HERE...
    print("Welcome to the Hangman game!")
    print('\n')
    print("My word has " + str(len(secretWord)) + " letters!")
    guesses = 8      # No. of guesses
    lettersGuessed = []   # Creating empty list
    Alletters = string.ascii_lowercase    # String containing all the lowercase letters
    while guesses > 0:    # Game starts
        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()     # Transfering input into lowercase
            lettersGuessed = lettersGuessed.append(letterslower)  # Inserting inputs into a list
            if letterslower not in Alletters:
                print("Opps! you have already guessed that letter: " + getGuessedWord(secretWord, lettersGuessed))
            else:
                if isWordGuessed(secretWord, lettersGuessed) == "True":
                    print("Congradualation! 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))







# a = "puck"
# b = ["f", "a", "c", "t"]
# print(isWordGuessed(a, b))
# print(getGuessedWord(a, b))
# print(getAvailableLetters(b))

a = "junior"
hangman(a)

3 个答案:

答案 0 :(得分:0)

第87行:lettersGuessed.append(letterslower)而不是lettersGuessed = lettersGuessed.append(letterslower)

追加不会返回任何内容

答案 1 :(得分:0)

您的错误是由此行引起的:

lettersGuessed = lettersGuessed.append(letterslower)

append方法可就地运行,并返回None,因此您的列表将变为NoneType,因为您将其设置为等于该调用。只是摆脱任务:

lettersGuessed.append(letterslower)

答案 2 :(得分:0)

你在哪里

lettersGuessed = lettersGuessed.append(letterslower)

您要将None分配给lettersGuessed,因为这是append返回的内容。

只需使用:

lettersGuessed.append(letterslower)