Skip to content

Instantly share code, notes, and snippets.

@KHerb
Last active November 10, 2016 05:31
Show Gist options
  • Save KHerb/9ded5dc2b4d6d2c6e49f9813c5e6e840 to your computer and use it in GitHub Desktop.
Save KHerb/9ded5dc2b4d6d2c6e49f9813c5e6e840 to your computer and use it in GitHub Desktop.
Guessing Game 2.0
import random
from random import randint
print "Welcome to guess a number 2.0 - This time I will try to guess a number that YOU pick in a range (0-100)"
def guess_game():
guess = randint(0,100)
new_list = [i for i in range(0,101)]
validate = raw_input("Is your number %d? (y/n)" % guess)
turn = 1
while validate == 'n':
turn += 1
question = raw_input("Was my guess too high or too low? (high/low)")
if question == 'high':
guess_list = [i for i in range(guess,101)]
for i in guess_list:
if i in new_list:
new_list.remove(i)
guess = random.choice(new_list)
validate = raw_input("Is your number %d? (y/n)" % guess)
elif question == "low":
guess_list = [i for i in range(0,guess+1)]
for i in guess_list:
if i in new_list:
new_list.remove(i)
guess = random.choice(new_list)
validate = raw_input("Is your number %d? (y/n)" % guess)
if validate == 'y':
print "Impressive. It took me %d tries to get it right!" % turn
guess_game()
@mishka245
Copy link

100 is less than 2^7=128. So, you can take numbers from the middle and your turns always will be less or equal than 7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment