Skip to content

Instantly share code, notes, and snippets.

@byanuaria
Created September 17, 2018 17:59
Show Gist options
  • Save byanuaria/f3360844fe89ed7f38dbe3343c8614dd to your computer and use it in GitHub Desktop.
Save byanuaria/f3360844fe89ed7f38dbe3343c8614dd to your computer and use it in GitHub Desktop.
Guessing number using bisection search
low = 0
high = 100
guess = (low + high) // 2
ans = ""
userInput = input('Please think of a number between 0 and 100!')
while True:
print('Is Your secret number ' + str(guess) + '?')
ans = 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 ans == 'c':
break
elif ans == 'h':
high = guess
guess = (high + low) // 2
elif ans == 'l':
low = guess
guess = (low + high) // 2
print('Game over. Your secret number was: ' + str(guess))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment