2014-09-20 55 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 “