这是我的代码:
def randWord():
"""opens a file of words and chooses a random word from the file"""
infile = open('dictionary.txt','r')
wordList = infile.read()
wordList2 = wordList.split('\n')
infile.close()
randWord = str(random.choice(wordList2))
return randWord
def hangman():
"""initiates the game by explaining the rules and terminates when game is over"""
global roundsWon
global roundsPlayed
print('\nWelcome to hangman! The rules are simple: A word will be chosen at random and will be represented by a sequence of blanks. Each blank constitutes a letter in the word. You will be asked to enter a letter and if the letter is contained in the word you will be notified. You can only make an incorrect guess 8 times before you lose the round. To win the round you must guess all the letters and reveal the word. Good luck!\n\n')
word = randWord()
while True:
guess = letterGuess(word)
if checkGuess(guess,word):
roundsWon += 1
roundsPlayed +=1
print('\nYou won! The word is {}.'.format(word))
break
elif guessesLeft == 0:
print("\nI'm sorry, but you have run out of guesses. The word was {}.".format(word))
roundsPlayed +=1
break
def letterGuess(word):
"""asks the user to guess a letter and prints the number of guesses left"""
blankedWord(word)
guess = input('\nGuess a letter: ')
return guess
def blankedWord(word):
"""converts the random word into the proper blanked form based on the letter guessed and lets the user know if their letter is in the word"""
displayWord=''
for letter in word:
if guessedLetters.find(letter) > -1:
displayWord = displayWord + letter #checks if the letter guessed is contained in the random word string by index.
print('\n{} is contained in the word!'.format(letter))
else:
displayWord = displayWord + '-'
print(displayWord)
def checkGuess(guess,word):
"""checks if the user enters a single letter guess or the full word"""
if len(guess) > 1 and len(guess) == len(word):
return completeWordGuess(guess,word)
else:
return oneLetterGuess(guess, word)
def completeWordGuess(guess,word):
"""analyzes the complete word guess to check if is correct"""
global guessesLeft
if guess.lower() == word.lower(): #kept it lower case for simplicity
return True
else:
guessesLeft -=1
return False
def oneLetterGuess(guess,word):
"""checks to see if the single letter guess is included in the whole word"""
global guessedLetters
global guessesLeft
global guessesMade
if word.find(guess) == -1: #checks for failure on .find function
guessesLeft -= 1
guessesMade += 1
print('\nThat letter is not in the word. You have made {} incorrect guesses and have {} guesses left.'.format(guessesMade,guessesLeft))
guessedLetters = guessedLetters + guess.lower()
if allGuessedLetters(word):
return True
return False
def allGuessedLetters(word):
"""checks if all of the letters in the word have been uncovered/guessed"""
for letter in word:
if guessedLetters.find(letter) == -1: #checks for failure on .find function
return False
return True
def gameStats():
"""prints the final statistics of a play session"""
print('\nYou have played {} games and you have won {} rounds!'.format(roundsPlayed,roundsWon))
import random
guessesMade = 0
guessesLeft = 8
roundsPlayed = 0
roundsWon = 0
guessedLetters = ''
userMode = 1
while userMode==1:
if userMode == 1:
hangman()
guessesLeft = 8
guessedLetters = ''
userMode = eval(input('\nEnter 1 to play again, type 0 to end the game: '))
else:
break
gameStats()
该程序似乎运行良好,除了一部分:如果用户猜到单词的第一个字母,程序将其视为完整的正确单词并将其视为胜利。所以如果我这个词是' rhino'我猜到了一个' r'它会显示为胜利。我没有看到错误,但是我感觉它在函数completeWordGuess中并且我不确定我是否应该为第一个条件返回True。任何帮助表示赞赏。
答案 0 :(得分:1)
我认为主要的问题 - 而且我说我想是因为我没有机会撕掉你的代码而寻找其他无法正常工作的方式,我可能不会 - 是你已经把你的回报错误地在allGuessedLetters中进行虚假调用。这就是你所拥有的:
def allGuessedLetters(word):
"""checks if all of the letters in the word have been uncovered/guessed"""
for letter in word:
if guessedLetters.find(letter) == -1: #checks for failure on .find function
return False
return True
问题在于,如果第一个字母没有返回False,则控制流会返回True,因为“return True”是for循环的每次迭代的一部分。方法不返回True的唯一方法是,如果第一个字母还没有被猜到。
如果你改变它:
def allGuessedLetters(word):
"""checks if all of the letters in the word have been uncovered/guessed"""
for letter in word:
if guessedLetters.find(letter) == -1: #checks for failure on .find function
return False
return True
该方法按预期工作,因为一旦遇到整个for循环并且每个字母都已被评估,控制流只会移动以返回True,如果它们中的任何一个不匹配则导致终止。