Write a program that chooses a random integer between 0 and 99 (inclu- sive). Then ask the user to guess what number has been chosen. Each time the user enters a guess, the program indicates whether the user guessed correctly (and exits), or if the guess was too high or too low. If you didn’t already know this, then you can tell Python to choose a random integer in any range with the randint function in the random module. Thus, you can say:


In [ ]:
import random
number = random.randint(10, 30)

In [ ]:
and number will contain an integer from 10 to (but not including) 30.

In [ ]:
import random

def guess_number():
    min = 0
    max = 99
    
    answer = random.randint(min, max)
    
    ok = False
    
    prompt = "Try to guess a number between {0} and {1}:".format(min, max)
    
    while not ok:
        
        try:
            guess = int(raw_input(prompt))
            if guess == answer:
                ok = True
                print "Bingo!"
                break
            elif guess > answer:
                prompt = "{0} is too much! Try a smaller number!".format(guess)
            else:
                prompt = "{0} too few! Try a larger number!".format(guess)
                
        except Exception as ex:
            prompt = "Apparently, it's not an integer number. Try once more."

guess_number()

In [ ]: