【发布时间】:2014-09-20 00:20:17
【问题描述】:
我正在尝试制作一个简单的刽子手游戏,但我无法显示猜错的字母和剩余的猜数。我们的代码可以确定哪些字母不正确,但我们需要将其打印出来。我们尝试将字母放入列表中,但我们不断收到错误消息“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)
【问题讨论】:
-
欢迎来到 Stack Overflow!要回答这个问题需要通过大量的代码。这使得如果不在问题上花费不成比例的时间,就很难给出好的答案。大多数人宁愿转向另一个问题。如果您将问题提炼到重现问题的尽可能小的样本,那么您获得的答案的数量、质量和清晰度也会有所提高。编辑后的问题不必和整个代码做同样的事情,它只需要重现你需要帮助的一个方面。
标签: python list loops global-variables range