我在这里是全新的,据我所知,没有人问过我的具体问题。如果有人有,请随时将我与问题联系起来。
无论如何,我正在阅读一本关于python book(发明你自己的电脑游戏)的发明,并且我正试图完成" Hangman"章节。我遇到的问题是,每当我输入一封信时,无论是否正确,该程序仍然会绘制刽子手的碎片。我目前正在运行Python 3,该书正在使用它。这是我的代码:
import random
HANGMANPICS = ['''
+---+
| |
|
|
|
|
===========''','''
+---+
| |
O |
|
|
|
===========''','''
+---+
| |
O |
| |
|
|
===========''','''
+---+
| |
O |
/| |
|
|
===========''','''
+---+
| |
O |
/|\ |
|
|
===========''','''
+---+
| |
O |
/|\ |
/ |
|
===========''','''
+---+
| |
O |
/|\ |
/ \ |
|
===========''']
words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar \
coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk \
lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot \
pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth \
snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat\
zebra'.split()
def getRandomWord(wordList):
wordIndex = random.randint(0, len(wordList)- 1)
return wordList[wordIndex]
def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
print(HANGMANPICS[len(missedLetters)])
print()
print('Missed letters:', end ='')
for letter in missedLetters:
print(letter, end = '')
print()
blanks = '_' * len(secretWord)
for i in range(len(secretWord)):
if secretWord[i] in correctLetters:
blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
for letter in blanks:
print(letter, end = '')
print()
def getGuess(alreadyGuessed):
while True:
print('Guess a letter.')
guess = input()
guess = guess.lower()
if len(guess) != 1:
print('Please enter a single letter')
elif guess in alreadyGuessed:
print('You have already guessed that letter. Choose again.')
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print('Please enter a LETTER')
else:
return guess
def playAgain():
print('Do you want to play again?(yes or no)')
return input().lower().startswith('y')
print('H A N G M A N')
missedLetters = ''
correctLetters = ''
secretWord = getRandomWord(words)
gameIsDone = False
while True:
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
guess = getGuess(missedLetters + correctLetters)
if guess in secretWord:
correctLetters = correctLetters + guess
foundAllLetters = True
for i in range(len(secretWord)):
if secretWord[i] not in correctLetters:
foundAllLetters = False
break
if foundAllLetters:
print('Yes! The secret word is "'+ secretWord +'"! You have won!')
gameIsDone = True
else:
missedLetters = missedLetters + guess
if len(missedLetters) == len(HANGMANPICS) -1:
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters))+ ' correct guesses, the word was "'+ secretWord +'"')
gameIsDone = True
if gameIsDone:
if playAgain():
missedLetters = ''
correctLetters = ''
gameIsDone = False
secretWord = getRandomWord(words)
else:
break
答案 0 :(得分:1)
主要问题在于您为丢失的信件设置了条件 - 即使是正确的猜测也会被视为错过的字母'。
在您的代码中:
else:
missedLetters = missedLetters + guess
在原始检查字母是否在单词之后,还有很长的路要走。
如果将子句移动到此处,则可以正常工作:
if guess in secretWord:
correctLetters = correctLetters + guess
foundAllLetters = True
else:
missedLetters = missedLetters + guess
在这种情况下,只有当错误的字母不在secretWord中时,它才会被添加到错过的字母列表中。
随着这种变化,事情似乎对我来说没问题。