2017-09-08 5 views
-1

私は二分法を使用して数値を探しています。私のコードの最初の部分は次のとおりです。エラーの結果は、/またはPythonで混ぜ合わせることによって導かれます

print("Please think of a number between 0 and 100!") 
     low = 0 
     high = 100 
     guess = int(0.5*(low + high)) 
     print("Is your secret number", guess,"?") 
     ans = 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.") 
     while ans != 'h' or ans != 'l' or ans != 'c': 
      print("Sorry, I did not understand your input.") 
      print("Is your secret number", guess,"?") 
      print(ans) 
      ans = 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.") 

ループが戻って続けている間、私は、その後、関係なく、私がキーボードに入れ、何のコードを実行しない場合:「申し訳ありませんが、私はあなたの入力を理解していませんでした」それから私に再度入力を求めてください。

誰でも私にこれがなぜ起こるのか教えていただけますか?

ありがとうございました!

+1

whileループでは 'および'の代わりに 'と'を使用する必要があります – SkrewEverything

答えて

2

である必要があり、それはあなたがする必要がどのような

を継続していきますので、あなたがFalseまたはTrueまたはTrue = Trueの時間

h!=h (False) 
h!=l(True) 
h!=c(True) 

を入力したとしましょう "やさん" に変更されあなたとあなたのコードでこれを行うと、そのうちの1つがFalseの場合、式全体がFalseになり、ループをもう一度実行することはありません:)

0

while ans != 'h' or ans != 'l' or ans != 'c':while ans != 'h' and ans != 'l' and ans != 'c':

+1

説明**理由**コードを修正するよりも初心者にとって重要です。 – SkrewEverything

+1

ああ、私は突然理解しています。ありがとう。 –

関連する問題