Created
September 17, 2018 17:59
-
-
Save byanuaria/f3360844fe89ed7f38dbe3343c8614dd to your computer and use it in GitHub Desktop.
Guessing number using bisection search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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