Skip to main content

challenge_guess_the_number_solution.py


"""
Challenge: guess the number game
Write a game in which the user has to guess a number (integer) from 0 to 10 (or a bigger range to make it harder).
Level 1: Generate a random integer to guess. The program will request a number until you find the correct one.
Level 2: This time, the computer will tell you weather the number you're trying to guess is higher or lower than the one you just entered.
"""
import random
range_min=0
range_max=50
target=random.randint(range_min,range_max)
# initial message
print 'Guess a number between',range_min,'and',range_max
# start with a wrong guess
guess=-1   # or choose a negative number
#
while guess != target :  # ask the question until you guess the answer
    guess=int(raw_input('number ?'))
    if (not range_min<guess<range_max):
        print 'The number should be between',range_min,'and',range_max,'. Try again'
    elif (target <  guess):
        print 'Lower'
    elif (target > guess):
        print 'Higher'
    elif (target == guess):
        print 'Hooray! You found the number!'
    else:
        print "I didn't catch that!"
# final message
print 'End of Game!'