Ввод не сопоставляется с предложением elif - PullRequest
0 голосов
/ 08 февраля 2020

Я новичок в изучении python и программировании в целом. Я написал этот код для компьютера, чтобы угадать число, которое я вообразил (между 1 и 100). Он показывает мне тот же вывод: «Извините, я не понял ваш вклад». это применимо, только если мой ввод не соответствует l, h или c. В тех случаях, когда мой вклад равен l, h или c, он должен принять эти условия и следовать, чтобы наконец достичь результата. Но этого не происходит. Я пытаюсь использовать метод деления пополам. Не могли бы вы помочь мне, где это идет не так?

num_begin = 0;
num_end = 100;
avg=(num_begin+num_end)/2

print("Please think of a number between 0 and 100!")

print("is your secret number "+ str(avg))
command=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 (True):
    if (command != 'c' or command != 'h' or command != 'l'):
        print("Sorry, I did not understand your input.")
        print("is your secret number "+ str(avg))
        command=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")

    elif(command=='l'):
        num_begin=avg
        avg=(num_begin+num_end)/2
        print("is your secret number "+ str(avg))
        command=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")

    elif(command=='h'):
        num_end=avg
        avg=(num_begin+num_end)/2
        print("is your secret number "+ str(avg))
        command=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")

    else:
        print("Game over. Your secret number was: " + str(avg))
        break

1 Ответ

1 голос
/ 08 февраля 2020

Просто замените или на и в вашем первом состоянии

 if (command != 'c' and command != 'h' and command != 'l'):
...