Skip to content

Instantly share code, notes, and snippets.

@jdell64
Last active August 29, 2015 14:25
Show Gist options
  • Save jdell64/572d262a3431c63aaab0 to your computer and use it in GitHub Desktop.
Save jdell64/572d262a3431c63aaab0 to your computer and use it in GitHub Desktop.
import random
import time
import sys
import os
# set the range for the user to guess
MIN_NUMBER = 1
MAX_NUMBER = 10000
def get_random(min, max):
""" Generates a random number. This function will take a minimum value and a maximum value and generate a random number between them."""
random_number = random.randint(min, max)
return random_number
def is_int(x):
try:
x = int(x)
return True
except ValueError:
return False
def how_close(guess, right_answer):
return right_answer - guess
#clear screen
os.system('cls') # on windows
print "Welcome. I am the Oracle."
print "I will think of a number between %s and %s. Guess it and you will win!" % (MIN_NUMBER, MAX_NUMBER)
while True:
user_answer = raw_input("Ready? <y/n> ")
if user_answer.lower().startswith('y' )
print "Great! Let's go."
break;
elif user_answer.lower().startswith('n')
print "Okay. Consult the Oracle when you are ready."
sys.exit()
print "Coming up with a number."
time.sleep(2)
oracles_number = get_random(MIN_NUMBER, MAX_NUMBER)
print "Got it!"
number_of_guesses = 0
while True:
number_of_guesses = number_of_guesses + 1
user_answer = raw_input("\nGuess> ")
# did the user type in a valid number?
user_answer_is_int = is_int(user_answer)
if user_answer_is_int:
# turn the string "user_anwser" into an int
user_answer = int(user_answer)
delta = how_close(user_answer, oracles_number)
if delta < 0:
print "Guess lower."
elif delta > 0:
print "Guess higher."
else:
print "You guessed it!"
break;
else:
print "That is not a whole number."
print "It took you %s guesses to guess my number." % (number_of_guesses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment