7
\$\begingroup\$

I'm starting to learn Python and am trying to optimize this bisection search game.

high = 100
low = 0
guess = (high + low)/2 
print('Please think of a number between 0 and 100!')
guessing = True
while guessing:
 print('Is your secret number ' + str(guess) + '?')
 pointer = 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 pointer == 'h':
 high = guess
 guess = (low + guess)/2
 elif pointer == 'l':
 low = guess
 guess = (high + guess)/2
 elif pointer == 'c':
 guessing = False
 else:
 print('Sorry, I did not understand your input.')
print('Game over. Your secret number was: ' + str(guess))
200_success
146k22 gold badges190 silver badges478 bronze badges
asked Feb 21, 2013 at 22:01
\$\endgroup\$
0

4 Answers 4

9
\$\begingroup\$

Some things I think would improve your code, which is quite correct:

  • Having variables for high and low, you shouldn't hard code their values in the opening print.
  • You should use // to make sure you are getting integer division.
  • You can write guess = (low + high) // 2 only once, if you place it as the first line inside the while loop.
  • When checking for pointer, you may want to first convert it to lower case, to make sure both h and H are understood.
  • Make your code conform to PEP8 on things like maximum line length.
  • Using the format method of str can make more clear what you are printing.

Putting it all together:

high, low = 100, 0
print('Please think of a number between {0} and {1}!'.format(low, high))
guessing = True
while guessing:
 guess = (low + high) // 2
 print('Is your secret number {0}?'.format(guess))
 pointer = 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.").lower()
 if pointer == 'h' :
 high = guess
 elif pointer == 'l' :
 low = guess
 elif pointer == 'c':
 guessing = False
 else:
 print('Sorry, I did not understand your input.')
print('Game over. Your secret number was {0}.'.format(guess))
answered Feb 22, 2013 at 0:44
\$\endgroup\$
7
\$\begingroup\$

In addition to Jaime's points.

  1. Get rid of the guessing flag, and just have an infinite loop with a break statement.

  2. pointer is a really wierd name for that variable especially as it means something else in other programming languages.

answered Feb 22, 2013 at 16:15
\$\endgroup\$
2
\$\begingroup\$

Completing the reply given by Jamie, with a remark:

When you type 'c' , even if the number is not the one you think of, always prints this part of code print('Game over. Your secret number was {0}.'

So, to avoid that, you must test also (str(numbers) == str(guess)) on the branch of (response == 'c'):

high, low = 100, 0
guess = (low + high) // 2
numbers = raw_input('Please think of a number between {0} and {1}!'.format(low, high))
guessing = True
while guessing:
 print('Is your secret number {0}?'.format(guess))
 response = 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.").lower()
 if response == 'h' :
 high = guess
 elif response == 'l' :
 low = guess
 elif (response == 'c') and (str(numbers) == str(guess)) : 
 print('Game over. Your secret number was {0}.'.format(guess))
 break
 else:
 print('Sorry, I did not understand your input.')
 guess = (low + high) // 2
answered Sep 7, 2015 at 10:31
\$\endgroup\$
1
  • \$\begingroup\$ The point of this guessing game is that the user never inputs the actual number since that is a secret. \$\endgroup\$ Commented Jun 16, 2017 at 5:04
1
\$\begingroup\$

In addition to the other answers: when I choose the number 100 as my secret, the code will run into and endless loop, repeatedly asking me how it compares to 99. You can restrict the range more after you get an answer from the user.

answered Jun 16, 2017 at 5:11
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.