1

I'm creating a program that asks you to think of a number from 0 to 100. Then it will guess if it IS 50, lower than 50, or higher than 50. The program will keep guessing with different numbers until it guesses the correct number.

If the user doesn't enter yes, lower or higher, then the output is supposed to be "I did not understand". The problem with my code is that it gets an infinite loop. I assume there is a loop and I need to include something to end it once the user says "yes".

Here is what I have so far (I'm a novice programmer so I'm sorry if this doesn't make sense or if my code is really bad!):

print('Hello.')
print('Pick a secret number between 0 and 100.')
print('Is your secret number 50')
low = 0
high = 101
guess = 50
a = input('Enter yes/higher/lower:\n')
while True:
    if a == 'yes':
        print('Great!')
        break
    elif a == 'lower':
        high = guess
        guess1 = (guess-low)//2+low
        print('Next is', guess1)
        print('Is your secret number', guess1)
    elif a == 'higher':
        low = guess
        guess1 = (high-guess)//2+guess
        print('Next is', guess1)
        print('Is your secret number', guess1)
    elif a != 'yes' or 'higher' or 'lower':
        print('I did not understand')
Whooper
  • 575
  • 4
  • 20
  • 3
    Move `a = input('Enter yes/higher/lower:\n')` to inside the `while` loop. – 001 Jan 31 '18 at 21:01
  • 1
    also that doesn't work as you expect: `elif a != 'yes' or 'higher' or 'lower':`: https://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values – Jean-François Fabre Jan 31 '18 at 21:02
  • why must you guys click the down arrow button for my post –  Jan 31 '18 at 21:03
  • https://stackoverflow.com/questions/39528456/guesses-a-secret-number-in-python – CristiFati Jan 31 '18 at 21:05
  • also pay attenion, you always updating high and low vars to initial guess var, not to updated value. – Aleks Lee Jan 31 '18 at 21:06
  • nvm I fixed it. I put the a = input('Enter yes/higher/lower:\n') inside the while loop and I changed the "guess1" to "guess". So thanks Johnny Mopp for that. –  Jan 31 '18 at 21:07
  • @Ali: No it's not, because the code doesn't work. – user2357112 Jan 31 '18 at 21:11
  • This isn't really that bad a question. It has correctly formatted, runnable code that reproduces the error when run, and a description of the desired and actual outputs. Since the code produces output indefinitely, it's not reasonable to expect a copy-paste of all the output, although an excerpt from the start would have helped (but it might be difficult for an inexperienced programmer to produce such an excerpt, depending on their environment). It could have used more detail regarding what input produces the problematic output and a more specific word than "MULTIPLE". – user2357112 Jan 31 '18 at 21:14
  • @user2357112 I deleted my comment for giving the wrong advice. Thanks for letting me know. – Ali Jan 31 '18 at 21:26
  • @CristiFati the link you've shared is technically similar with the program in this post. However, this post is more well put. He encountered a problem with his code and he did describe what was happening and is asking for help to find out what's wrong with it and how can it be fixed. :) – Whooper Jan 31 '18 at 22:02

1 Answers1

0

Try out this code and see if this is the intended behavior of your program.

print('Hello.')
print('Pick a secret number between 0 and 100.')
print('Is your secret number 50')
low = 0
high = 100

while True:
    guess = int((low+high)/2)
    print('Is your secret number', guess)
    a = input('Enter yes/higher/lower:\n')
    if a == 'yes':
      print('Great!')
      break
    elif a == 'lower':
      high = guess-1
    elif a == 'higher':
      low = guess+1
    else:
        print('I did not understand')

The algorithm goes like this:

  1. You make your guess by getting the middle of the low and high values. So you do that with guess = int((high+low)/2)
  2. If the user's number is greater than your guess, then you change your low value to guess+1. If the user's number is lower than your guess, then you change your high value to guess-1.
  3. Repeat this process until you eventually get the user's number.
Whooper
  • 575
  • 4
  • 20
  • 2
    are you sure you're thinking of python 3? I'm learning it now and I've always used just "input()" and it works fine for me. I never used "raw_input()". I know int(input()) works for integers. –  Jan 31 '18 at 21:11
  • Oh yes my bad! Just move your `a = input('Enter yes/higher/lower:\n')` inside the while loop to avoid the infinite loop. – Whooper Jan 31 '18 at 21:15
  • @rainydayss check out my updated code and see if it works exactly as you want it to be. – Whooper Jan 31 '18 at 21:33