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))
4 Answers 4
Some things I think would improve your code, which is quite correct:
- Having variables for
high
andlow
, you shouldn't hard code their values in the openingprint
. - 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 thewhile
loop. - When checking for
pointer
, you may want to first convert it to lower case, to make sure bothh
andH
are understood. - Make your code conform to PEP8 on things like maximum line length.
- Using the
format
method ofstr
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))
In addition to Jaime's points.
Get rid of the
guessing
flag, and just have an infinite loop with a break statement.pointer
is a really wierd name for that variable especially as it means something else in other programming languages.
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
-
\$\begingroup\$ The point of this guessing game is that the user never inputs the actual number since that is a secret. \$\endgroup\$Roland Illig– Roland Illig2017年06月16日 05:04:20 +00:00Commented Jun 16, 2017 at 5:04
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.
Explore related questions
See similar questions with these tags.