2016-06-20 10 views
0

だから私はこのコードを持って終了していないが、それはそれは「H」「L」または 'Cとの数やユーザーの回答を促し0から100 間の数を推測しループが適切に

今すべてがうまくいきます。ただし、ユーザー入力が 'h'、 'l'または 'c'と等しくないときにのみこの行を表示したい場合は、ループを破る 'c'を入力しても表示されます?

print "Sorry, I did not understand your input." 

完全なコード

start = 0 
end = 100 
guess = int((start+end)/2.0) 
print "Please think of a number between 0 and 100!" 
print 


x= 'n' 

while x != 'c': 

    print 'Is your secret number ' + str(guess) + '?' 
    x = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.").lower() 
    if x == 'h': 
     end = guess 
    elif x == 'l': 
     start = guess 
    else: 
     print "Sorry, I did not understand your input." 
guess = int((start + end)/2) 

print 'your answer is: ' + str(guess) 

だから、これを明確にするため、私は

Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess  is too low. Enter 'c' to indicate I guessed correctly.c 

Sorry, I did not understand your input. 

'C' を入力しても出力は、あなたの答えはです:あなたが持っていない50

答えて

0

'c'の場合はelifなので、最終的にはelseになります。

追加、そのケースを処理し、whileループを破るために、以下:また

elif x == 'c': 
    break 

、あなただけのc破断するまでwhile True:とループを行うことができます。

あなたの上位/下位ロジックも逆です。 hの場合は、start = guessとなります。

guessへの更新は、whileループ内でインデントする必要があります。

+0

ありがとうございました!さて、私は最後に、そのブレークが使われていることを理解しています、私は混乱しました。 私は最終的に私のコースを続けることができます。多くの愛! – Kyriediculous