2017-02-14 9 views
0

このコードは、単語が正しく推測されるか、それ以上人生が残るまで、推測を関数に入力できるようにするにはどうすればよいですか?現在、ユーザーは1つの文字列しか入力できず、ループは突然終了します。私のコードが正しくループすることができません

secret_words_list = ['voldemort', 'hogwarts'] 
def hangman(): 
    lives = 5 
    while lives >= 0: 
     answer = random.choice(secret_words_list) 
     guess = raw_input('Write your answer here: ') 
     hangman_display = '' 
     for char in answer: 
      if char in guess: 
       hangman_display += char 
       lives -= 1 
      elif char == ' ': 
       hangman_display += char 
      else: 
       hangman_display += "-" 
       lives -= 1 
     if hangman_display == answer: 
      print("You win") 
    print(hangman_display) 
+2

あなたは ''以外のすべての文字について人生を離れている可能性があります。 – Julien

+0

多分あなたはこれを見ることができます: caimaoy

+0

それぞれのチェックのために人生を減らしました。 forループの後(1回の繰り返しで1回)? – Vinay

答えて

0
import random 
secret_words_list = ['voldemort', 'hogwarts'] 
def hangman(): 
    lives = 5 
    while lives >= 0: 
     answer = random.choice(secret_words_list) 
     guess = raw_input('Write your answer here: ') 
     hangman_display = '' 
     for char in answer: 
      if char in guess: 
       hangman_display += char 
       #lives -= 1 
      elif char == ' ': 
       hangman_display += char 
      else: 
       hangman_display += "-" 
       #lives -= 1 
     if hangman_display == answer: 
      print("You win") 
      break 
     else: 
      lives -=1 
    print(hangman_display) 

hangman() 

私はあなたの正確な要件を理解しdin'tが、これはあなたが探しているものでしょうか?

それは文字単位で文字にチェックではなく、推測が間違っていたかどうかの決定後、単語全体をチェックしているため、プログラムの相互作用が

Write your answer here: vol 
-o------ 
Write your answer here: hog 
hog----- 
Write your answer here: hogwart 
hogwart- 
Write your answer here: hogwarts 
You win 
hogwarts 
0

、以下のようなものだったことが突然終了する理由はあります。生命が奪われる必要があるかどうか、基本的

Here's my code for this solution, documented so you can understand:

、スイッチとして動作する変数を持っている、あなたは正しい推測を持っているときにそれを切り替えて、その後、確認するために「の」ループの後のチェックを持っていますない。

これは私がループの前に作成した「正しい」変数でやっていることがわかります。

ホープこれは^役立ちます^ コナー

編集:P

チェックコードの場合:私は、それは巨大なダンプではありませんように、少しこれを打破するつもりだ

あなたはこれを理解できません。

あなたは入力を受け取り、それが1文字であることをテストし、ディスプレイでdoohickeyを実行します。私はの話「スイッチ」が作成される場所

各文字の上にあるチェック...

#we need to check if we need to take a life away 
correct = False 

これは、単なるブール変数です。

#loop through the word to guess, character by character. 
for char in word: 
    #if the character is in the old display, add it to the new on. 
    if char in lastdisplay: 
     display += char 

ここで、文字が事前に表示された場合は、新しいディスプレイに表示されます。

#if the character is in the guess, add it to the display. 
    elif char in guess: 
     display += char 
     #we made a correct guess! 
     correct = True 

推測文字が、我々は現在、ディスプレイにそれを追加し、「真の」

#otherwise we need to add a blank space in our display. 
    else:    
     if char == ' ': 
      display += ' ' #space 
     else: 
      display += '_' #empty character 

にスイッチを入れるチェックしているそうでなければ、何事もなかった、空間に追加文字である場合/ blankとループを続けます。

#if we didn't get a correct letter, take a life. 
if not correct: 
    lives -= 1 

ここではそれ以外の場合は 『偽「スイッチがある』と私たちは命を奪う私たちは「スイッチ」をチェックし、それが「本当」であるならば、私たちは命を取る必要はありません、

たのです。

+0

ありがとうございます!!!!! –

関連する問題