0

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....

KGS
  • 277
  • 2
  • 4
  • 11
  • Thanks for the info on conditionals. I forwent the list because we aren't up to them in class yet, and I added the calculation of guess as guess itself. – KGS Nov 26 '13 at 22:48

3 Answers3

1

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.')
jramirez
  • 8,537
  • 7
  • 33
  • 46
0

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.')
thegrinner
  • 11,546
  • 5
  • 41
  • 64
0

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))
uselpa
  • 18,732
  • 2
  • 34
  • 52
  • Thanks for the feedback. Can you point out the other logical errors? – KGS Nov 26 '13 at 22:17
  • Guess should be (High **+** Low)/2. And you don't stop when the program keeps guessing the same number. – uselpa Nov 26 '13 at 22:18
  • Also, there's not good reason to compute Guess in more than one place. – uselpa Nov 26 '13 at 22:22
  • You seem to use Python 2, so you need to use `raw_input()` as in your example. I used Python 3 which is why I simply use `input()` but this also requires to use `int()` to make sure Guess is an integer. – uselpa Nov 26 '13 at 22:25
  • ok thanks. I accidentally pasted an old version of code with high-low, but the compute guess in more than one place is a good point thanks. – KGS Nov 26 '13 at 22:34