Question

I'm wrote some code to determine a secret number between 0 and 100. The user tells the machine the guessed number (which is half the range) is either to high, too low or just right. Based on the input, the machine used bisection search to adjust the guess. When the guess is correct, the user presses c and the game ends. The problem is, in spite of the conditions placed in the 'i did not understand input' branch, this branch triggers when the user presses c ( a valid entry) and it is not the first guess.

for example, here is the output-

Please think of a number between 0 and 100!
Is your secret number 50?
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. l
Is your secret number 75?
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.
Game over. Your secret number was:75
>>> 

And here is the code-

    High=100
    Low=0
    Guess=50
    user_input=0

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

    while user_input != 'c':
        print("Is your secret number"+" "+str(Guess)+"?")
        userinput = 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.")
        if user_input == 'h':
            High=Guess
            Guess= ((High+Low)/2)
        if user_input == 'l':
            Low=Guess
            Guess= ((High+Low)/2)
        if user_input != 'h' or 'l' or 'c':
            print('Sorry, I did not understand your input.')

    print ('Game over. Your secret number was:'''+ str(Guess))

Thanks in advance. I'v been wracking my head over this for hours....

Was it helpful?

Solution 2

Conditionals don't work like that. You need something like:

# Check each condition explicitly
if user_input != 'h' and user_input != 'l' and user_input != 'c':

Or:

# Check if the input is one of the elements in the given list
if user_input not in ["h", "c", "l"]:

Your current approach is understood as

if (user_input != 'h') or ('l') or ('c'):

And since l and c are truthy, that branch will always execute.


You might also consider using elif, so your conditions would become the following:

while True:
    if user_input == 'h':
        High=Guess
        Guess= ((High-Low)/2)
    elif user_input == 'l':
        Low=Guess
        Guess= ((High-Low)/2)
    elif user_input == "c":
        # We're done guessing. Awesome.
        break
    else:
        print('Sorry, I did not understand your input.')

OTHER TIPS

Try this instead for that conditional.

if user_input not in ['h','l','c']:
      print('Sorry, I did not understand your input.')

You probably don't have to check if user_input is h or l since the first couple of if should handle that.

    if user_input == 'h':
        High=Guess
        Guess= ((High-Low)/2)
    elif user_input == 'l':
        Low=Guess
        Guess= ((High-Low)/2)
    elif user_input == 'c':
        pass # the while statement will deal with it or you could break
    else:
        print('Sorry, I did not understand your input.')

Other than your if, your logic has a few errors. I'd recommend something like this:

High = 100
Low = 1
LastGuess = None

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

while True:
    Guess = int((High+Low)/2)
    if Guess == LastGuess:
        break
    print("Is your secret number"+" "+str(Guess)+"?")
    user_input = 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.")
    if user_input == 'h':
        High = Guess
        LastGuess = Guess
    elif user_input == 'l':
        Low = Guess
        LastGuess = Guess
    elif user_input == 'c':
        break
    else:
        print('Sorry, I did not understand your input.')

print ('Game over. Your secret number was:'''+ str(Guess))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top