Guess a Big Number
# GuessBigNumber.py
#
# Guess a number from 1 to 100
#
import random
answer = random.randint(1,100)
intro = """
I'm thinking of a number from 1 to 100.
Try to guess it in the fewest tries.
Enter your first guess: """
def getNumber(prompt):
n = input(prompt)
while not n.isdigit():
n = input("That isn't a number! Try again:")
return int(n)
def nextGuess(n, dir):
msg = "You guessed " + str(n) +\
". That is too " + dir +\
".\n\nGuess again: "
return getNumber(msg)
guess = getNumber(intro)
tries = 1
while guess != answer:
if guess > answer:
direction ="high"
else:
direction = "low"
guess = nextGuess(guess, direction)
tries += 1
print("Right, it was", answer)
print("You guessed it in", tries, "tries! Hooray!")