2014-09-20 54 views
0

我想做一個簡單的hang子手遊戲,但我有麻煩顯示不正確的猜測字母和剩下的猜測量。我們的代碼可以確定哪些字母不正確,但我們需要打印出來。我們嘗試將這些字母放入列表中,但我們不斷收到錯誤消息'NoneType'對象不可迭代。此外,每當我們運行guess()時,我們也會繼續得到負面猜測,一旦猜測次數達到0,遊戲就沒有結束。你能幫我找到問題嗎?Hang子手遊戲擴展/附加列表

# 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) 
+5

歡迎來到Stack Overflow!要回答這個問題需要經歷很多代碼。如果不在這個問題上花費過多的時間,就很難給出正確的答案;大多數人寧願轉向另一個問題。如果將問題歸結爲可能再現問題的最小樣本,那麼您得到的答案的數量,質量和清晰度也會提高。編輯後的問題不必與整個代碼做同樣的事情,只需要重現需要幫助的一個方面。 – Veedrac 2014-09-20 00:25:44

回答

0

問題之一: 你看到「遊戲結束」時,你猜一個字母。爲什麼?

你忘了做:global numGuessesRemaining行後21

您使用您將其設置爲9號線的價值,所以你的整個程序認爲numGuessesRemaining應該是0,所以isGameOver正確地說:「GAMEOVER 「