我正在通过“使用Python发明”来学习Python。对于Python 2.7。目前正致力于Hangman章节。
我在过去3天内完成了5次代码,但是在运行代码时我没有得到相同的结果。
我的代码:
import random
HANGMANPICS = ['''
+------+
| |
| |
|
|
|
|
|
|
|
|
==============''', '''
+ +
| |
| |
O |
|
|
|
|
|
|
|
==============''', '''
+------+
| |
| |
| |
O |
| |
| |
| |
|
|
==============''', '''
+------+
| |
| |
O |
/| |
/ | |
| |
|
|
|
|
==============''', '''
+------+
| |
| |
O |
/|\ |
/ | \ |
| |
|
|
|
|
==============''', '''
+------+
| |
| |
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
有人可以告诉我问题是什么 -
请帮忙。
答案 0 :(得分:0)
Python是“对空间敏感的”。所以这段代码
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
子句相关(if foundAllLetters:
)。删除4个空格,因此else
与if guess in secretWord
位于同一列。