Skip to content

Instantly share code, notes, and snippets.

@PerroBueno
Created August 9, 2018 22:50
Show Gist options
  • Save PerroBueno/5661cb18995d01f5f127f265d1a4ff3e to your computer and use it in GitHub Desktop.
Save PerroBueno/5661cb18995d01f5f127f265d1a4ff3e to your computer and use it in GitHub Desktop.
Guesses a number that you think of
#Number Guesser 2
print("Think of a number between 1 and 100.")
def checker(guess):
nums = [1,2,3]
print("Number is "+ str(guess) + ". Is my answer:")
print("[1] correct")
print("[2] Too high")
print("[3] Too low")
while True:
try:
response = int(input())
if response in nums:
break
else:
print("Enter [1] [2] or [3]!")
except ValueError:
print("Enter [1] [2] or [3]!")
return response
#Initial guess and limits
guess = 50
high = 100
low = 1
counter = 0
while True:
response = checker(guess)
counter += 1
if response == 1:
print("I Win! I took " + str(counter) + " attempts!")
break
elif response == 2:
high = guess
else:
low = guess
guess = int((high + low)/2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment