Hangman游戏扩展/附加列表

时间:2014-09-20 00:20:18

标签: python list loops global-variables range

我正在尝试制作一个简单的刽子手游戏,但是我无法显示错误猜到的字母和剩下的猜测量。我们的代码可以确定哪些字母不正确,但我们需要打印出来。我们尝试将字母放入列表但我们不断收到错误消息“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)

1 个答案:

答案 0 :(得分:0)

其中一个问题: 你正在看" Game Over"每当你猜一封信。为什么呢?

你忘记了:第21行之后global numGuessesRemaining

您正在使用您在第9行设置的值,因此您的整个程序认为numGuessesRemaining应为0,因此isGameOver正确地说" GameOver&#34 ;