Skip to content

Instantly share code, notes, and snippets.

@MSourceCoded
Created October 23, 2014 13:28
Show Gist options
  • Save MSourceCoded/7abef1fefc4df4e3f1c8 to your computer and use it in GitHub Desktop.
Save MSourceCoded/7abef1fefc4df4e3f1c8 to your computer and use it in GitHub Desktop.
Python Number Guessing Game
# Number Guessing Game
# Binary Search method
# (go easy on me, I'm new with python)
def search(lowerBound, higherBound):
halfPoint = int((higherBound - lowerBound) / 2 + lowerBound) # Get the midpoint of the range, and translate it to the proper position (getting halfway)
print("Is your number: ", halfPoint) # Get the halfway point status from the player
result = input("")
if result == "H" or result == "h":
lowerBound = halfPoint # Set the lower boundary to the halfway point
elif result == "L" or result == "l":
higherBound = halfPoint # Set the higher boundary to the halfway point
elif result == "Y" or result == "y":
print("Woohoo!")
input("") # Reset
return
search(lowerBound, higherBound) # Call the function over and over again until we find the number. This is recursion.
while(True):
print("Welcome to Guess the Number")
print("Pick one between 0-1000.")
print("Type 'H' if your number is HIGHER")
print("Type 'L' if your number is LOWER")
print("Type 'Y' if the guess was correct")
print("Go ahead, type something when you are ready!")
input("")
search(0, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment