-1

I am trying to make a guessing game in python using bisection search. my code goes like this.

print('please think of a number between 1 and 100')
high=100
low=1
guess=int((high+low)/2)
n=input('is your number 50? press y to yes, l to low, h to high ')
while n!='y':
    if n=='h':
        high=guess
        n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
    elif n=='l':
        low=guess
        n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
        print() 
    else:
        n=input('wrong input. press y to yes, l to low, h to high ')
        print()
    guess=int((high+low)/2)
print('game over. i guessed correctly')

But unfortunately, I dont get desired things out of this code. Would you mind to help me? Thanks in advance

  • 4
    What do you expect and what is the result? – ImAtWar Dec 14 '17 at 08:29
  • 3
    You need to explain the *desired things out of this code* to get a solution. – bhansa Dec 14 '17 at 08:31
  • A couple things: (1) Update `guess` *before* prompting with it in your `input` calls, otherwise you're showing the *previous* guess, and (2) Use `//` instead of `/` when dividing, otherwise you will end up with a floating point guess. – Tom Karzes Dec 14 '17 at 08:32
  • that was a silly mistake. here i am trying to explain my desired things. first of all, my program will let the user to think of a number. after that it will ask the person whether his number is (high+low)/2 [point to be noted, you will understand the value of high and low respectively by going through my code.]and it will continue like this changing the values of high and low. after getting y as input, the program will stop. hopefully you all understand – Ahanaf Akif Pathan Dec 14 '17 at 08:36
  • 1
    You have another bug: (or perhaps not depending on your meaning of 'between 1 and 100'. If a user picks 100, your script will never end, due to how `int(number)` works: basically, if you pick 100, you end up in a situation of high = 100, low = 99, then 99+100 / 2 == 99.5. Then int(99.5) == 99. So you get stuck in a loop. https://tio.run/##ZVDPC4IwGL3vr3gZgqKQ2U3qXIfo1F2kllvYJm5i/vXrm3YIgw3et/fje6wdrdBq55yQtcAB2yxjjR48YnXPjSEklY2iiU9AXIwN8pixNa5CGtB59sZC8I7DahirW1SKXA@ppOVk0S17Ve/SdmN5072yvKPUjLFByIZjXrOadpPxjqV2jzwrGIC52KT341@/5Kcd8W3nmeDCBxy9tMBZDwVCgxOJJ/B9D02AEBHZU/igdM6OY/gcf5edEv9Bzn0A – Jmons Dec 14 '17 at 09:08

4 Answers4

2

you should update guess before showing prompt to user in your while loop.

while n!='y':
    if n=='h':
        high=guess
        guess=int((high+low)/2)
        n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
    elif n=='l':
        low=guess
        guess=int((high+low)/2)
        n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
        print() 
    else:
         n=input('wrong input. press y to yes, l to low, h to high ')
         print()
Hamid Sajjadi
  • 113
  • 2
  • 10
0

So the problem was that you were not updating the "guess" after changing the high/low but before printing it to the user.

Here's the modified code.

Note, I also changed the language of the message to make it clearer to the user when they should type "h" and when they should type "l"

print('please think of a number between 1 and 100')
high=100
low=1
guess=int((high+low)/2)
input_options = 'press "y" if yes, "l" if it is lower, "h" if it is higher: '
n=input('is your number '+str(guess)+'? ' + input_options)

while n!='y':
    if n=='h':
        low=guess
        guess=int((high+low)/2)
        n=input('is your number '+str(guess)+'? ' + input_options)
    elif n=='l':
        high=guess
        guess=int((high+low)/2)
        n=input('is your number '+str(guess)+'? ' + input_options)
    else:
        n=input('wrong input. ' + input_options)

print('game over. i guessed correctly')
Pranay Majmundar
  • 803
  • 7
  • 15
0

There is probably an error with the update of guess too, I mean, when the guess is lower than the expected value you should guess a new value from the current guess to previous high, so just change the value of low :

if n=='h':
    low=guess
    guess=int((high+low)/2)
Fou
  • 161
  • 1
  • 9
0

Expecting it to be a binary search scenario I did little changes for optimization.

print('please think of a number between 1 and 100')
high=100
low=1
guess=int(low+(high-low)/2)
n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
while n!='y':
    if n=='h':
        low=guess
    elif n=='l':
        high=guess
    else:
        n=input('wrong input. press y to yes, l to low, h to high ')
    guess=int(low+(high-low)/2)
    n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
print('game over. i guessed correctly')
Ronny
  • 23
  • 3