import random def number_guessing_game(): """ Play a number guessing game where the user tries to guess a number between 1 and MAX_GUESS and is given hints for lower and higher """ MAX_GUESS = 20 number = random.randint(1, MAX_GUESS) correct = False while not correct: guess = int(raw_input("Guess a number between 1 and " + \ str(MAX_GUESS) + ": ")) if guess == number: print "Good job!" correct = True elif guess < number: print "A bit higher" else: print "A bit lower" # here's an alternate way of writing it that doesn't use a # bool variable. Either way is fine. def number_guessing_game2(): """ Play a number guessing game where the user tries to guess a number between 1 and MAX_GUESS and is given hints for lower and higher """ MAX_GUESS = 20 number = random.randint(1, MAX_GUESS) guess = int(raw_input("Guess a number between 1 and " + \ str(MAX_GUESS) + ": ")) while guess != number: if guess < number: print "A bit higher" else: print "A bit lower" guess = int(raw_input("Guess a number between 1 and " + \ str(MAX_GUESS) + ": ")) # when we exit the loop, we know the user's gotten it right print "Good job!"