2016-11-30 62 views
1

我正在開發一個特定的項目,它有一個bug。該劇再次適合'不'。但是,這將顯示每次我輸入「是」:Python中的hang子手遊戲

TIME TO PLAY HANGMAN 
Do you want to play again (yes or no)? 

這裏是我的全部代碼

import random 

def Hangman(): 
print ('TIME TO PLAY HANGMAN') 

wordlist =['apples', 'oranges', 'grapes', 'pizza', 'cheese', 'burger'] 
secret = random.choice(wordlist) 
guesses = 'aeiou' 
turns = 5 

while turns > 0: 
    missed = 0 
    for letter in secret: 
     if letter in guesses: 
      print (letter,end=' ') 
     else: 
      print ('_',end=' ') 
      missed= missed + 1 

    print 

    if missed == 0: 
     print ('\nYou win!') 
     break 

    guess = input('\nguess a letter: ') 
    guesses += guess 

    if guess not in secret: 
     turns = turns -1 
     print ('\nNope.') 
     print ('\n',turns, 'more turns') 
     if turns < 5: print ('\n | ') 
     if turns < 4: print (' O ') 
     if turns < 3: print (' /|\ ') 
     if turns < 2: print (' | ') 
     if turns < 1: print ('/\ ') 
     if turns == 0: 
      print ('\n\nThe answer is', secret) 

playagain = 'yes' 
while playagain == 'yes': 
    Hangman() 
    print('Do you want to play again? (yes or no)') 
    playagain = input() 
+0

我甚至無法讓你的代碼運行。它在各地都被打破了。 – DejaVuSansMono

+0

你只需要縮進大部分的代碼,這樣它的hangman函數的一部分 – Navidad20

回答

2

如果代碼看起來這裏像它在你的編輯器,你的問題是你有沒有在print('TIME TO PLAY HANGMAN')之後縮進了所有代碼,所以python認爲它在外部範圍內,只執行一次。它需要看起來像:

def Hangman(): 
    print ('TIME TO PLAY HANGMAN') 
    wordlist =['apples', 'oranges', 'grapes', 'pizza', 'cheese', 'burger'] 
    # etc. 

playagain = 'yes' 
while playagain == 'yes': 
    # etc. 
1

Hangman函數做的唯一的事情就是打印「時間玩劊子手」。其他一切都在功能之外。修正你的縮進以將遊戲循環放入函數中,它應該起作用。

0

你停留在while循環:

playagain = 'yes' 
while playagain == 'yes': 
    Hangman() 
    print('Do you want to play again? (yes or no)') 
    playagain = input() 

你的循環在不斷地尋找,看是否playagain == 'yes'。既然你輸入了yes,while循環運行的條件仍然是真的,這就是爲什麼它再次運行並打印你的語句。

我沒有運行你的代碼或檢查它的其餘部分,但根據你給的問題,這應該是你的修復。