2013-10-08 65 views
-2

我正在通過Python的'Invent With Python'來學習Python 2.7。目前正在執行Hang子手章節。我的Hang子手遊戲出現錯誤

我在過去的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 

有人能告訴我是什麼問題 -

  1. 按我的理解,我搞亂了縮進。就是不知道在哪裏。

請幫忙。

+3

這對於單個問題來說太過分了。提取[SSCCE](http://sscce.org/),並描述一個具體問題。 – BartoszKP

+0

我還不知道什麼是SSCCE。感謝您的鏈接! –

回答

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個空格,以便elseif guess in secretWord位於同一列。

+0

我的朋友提到了這個網站,並說:「有些可怕的人在那裏呆着,你會得到很好的幫助。」哇,我不知道他字面上的意思!謝謝約翰。不到30分鐘即可完成解決方案。已經愛過了!我沒有太多的代表,或者我會投你的答案。 –

+0

@SocialCoder你還不能投票,但是如果能解決你的問題,一定要接受這個答案。 – BartoszKP