Hangman游戏扩展/附加列表



我正试图制作一个简单的刽子手游戏,但我在显示猜错的字母和剩余的猜测量时遇到了问题。我们的代码可以确定哪些字母不正确,但我们需要打印出来。我们尝试将字母放入列表中,但不断收到错误消息"NoneType"对象不可迭代。此外,每当我们运行guess()时,我们也会不断得到负面的猜测,一旦猜测次数达到0,游戏就不会结束。你能帮我找出问题吗?

# Hangman Game`
import random
words = open('words.txt').readlines()  # List of words read in from file
secret = ''  # Secret word the player is trying to guess
correctLetters = ''  # Correctly guessed letters so far in secret word
numGuessesRemaining = 0  # Number of incorrect guesses remaining
incorrectGuesses = ''  # String containing incorrectly guessed letters
# Returns a randomly chosen word, all letters uppercased, from the word list
def getNewSecretWord():
    return words[random.randint(0, len(words)-1)].upper().strip()
# Initializes a new game.Sets *secret* to be a new randomly chosen word.Sets *correctLetters* to be a string of '-' characters of the same length as *secret* Sets *numGuessesRemaining* to be 8. Sets *incorrectGuesses* to be empty. Prints out the game board.
def newGame():
    print('Starting hangman game with new secret word')
    global secret
    secret = getNewSecretWord()
    print(secret)
    numGuessesRemaining = 8
    incorrectGuesses = ''
    global correctLetters
    correctLetters = '-' * len(secret)
    printGameBoard(incorrectGuesses,numGuessesRemaining,correctLetters)
# Returns a new string, identical to s, except that the character at the specified index is replaced with the specified new character.
def replaceCharacterAtIndexInString(s,index,newCharacter):
    name = list(s)
    name[index]= newCharacter 
    return ''.join(name)
# Prints out the game board. The game board consists of the set of incorrectly guessed letters, the number of guesses remaining, and the letters guessed correctly thus far in the secret word.
def printGameBoard(incorrectGuesses,numGuessesRemaining, correctLetters):
    print('Incorrect guesses so far: ' + ''.join(incorrectGuesses))
    print(str(numGuessesRemaining) + ' guesses remaining')
    print(correctLetters)
# Converts the specified letter to uppercase and checks if it is in the secret word. If it is not in the secret word, *numGuessesRemaining* is decremented and the letter is added to the string of *incorrectGuesses*. If it is in the secret word, the index of each occurrence of the letter in the secret word is determined and, for each index, characters (hyphens) in the string *correctLetters* are replaced by the specified letter at the corresponding index. After checking whether the letter is in the secret word, the game board  is printed out and a check is performed to see if the game is over.
def guess(letter):
    letter = letter.upper()
    for num in range(0, len(secret)):
        if letter == secret[num]:
            global correctLetters
            correctLetters = replaceCharacterAtIndexInString(correctLetters,num,letter)
            isGameOver()
        else:
            global incorrectGuesses
            wrongGuesses = list(incorrectGuesses) #NonType error message
            letters = list(letter)
            incorrectGuesses = wrongGuesses.extend(letters)
            global numGuessesRemaining
            numGuessesRemaining = numGuessesRemaining - 1
            isGameOver()
    printGameBoard(incorrectGuesses,numGuessesRemaining,correctLetters)
# Checks if the game is over and if so prints an appropriate message. The game is over if all letters in the secret word have been correctly guessed, in which case the player wins, or if the all guesses have been used up, in which case the player loses.
def isGameOver():
    if correctLetters == secret: 
        print ('Congratulations, you win!')
    if numGuessesRemaining == 0:
        print ('You are out of guesses. You lose. The secret word was: ' 
        + secret)

其中一个问题:每当你猜到一封信时,你就会看到"游戏结束"。为什么?

您忘记执行:第21行之后的global numGuessesRemaining

您使用的是在第9行设置的值,因此整个程序认为numGuessesRemaining应该为0,因此isGameOver正确地表示"GameOver"

相关内容

  • 没有找到相关文章

最新更新