import random
HANGMANPICS = ['''
+------+
| |
| |
|
|
|
|
|
|
|
|
==============''', '''
+ +
| |
| |
O |
|
|
|
|
|
|
|
==============''', '''
+------+
| |
| |
| |
O |
| |
| |
| |
|
|
==============''']
words = 'ant baboon badger bat bear beaver beetle bird camel cat clam cobra cougar coyote crab crane crow deer dog donkey duck eagle ferret fish fox frog goat goose hawk iguana jackal koala leech lemur lion lizard llama mite monkey moose moth mouse mule newt otter owl oyster panda parrot pigeon python quail rabbit ram rat raven rhino salmon seal shark sheep skunk sloth slug snail snake spider squid stork swan tick tiger toad trout turkey turtle wasp weasel whale wolf wombat worm zebra'.split()
# This function returns a random string from the list of strings.
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:',
for letter in missedLetters:
print letter,
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,
print
def getGuess(alreadyGuessed):
while True:
print 'Guess a letter.'
guess = raw_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 raw_input().lower().startswith('y')
print 'HANGMAN'
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
在第二个函数 displayBoard 中,传递了 4 个参数。我了解 HANGMANPICS、missedLetters 和 secretWord,但我不明白正确字母的参数在哪里定义/替换。
用外行的话(因为我是新手),它在哪里说正确的字母应该做什么?我已经研究这个游戏一个星期了,到目前为止一切似乎都很好,除了这个。
请帮忙。