# Play a guessing game with the user. The goal is for the user to figure # out which number between 1 and 100 that the computer chose in as few # guesses as possible. import random # Pick the target number that the user is trying to guess upper_limit = 100 lower_limit = 1 target = random.randrange(lower_limit, upper_limit+1) # Read the guess from the user guess = int(input("Enter an integer between " + str(lower_limit) + " and " + str(upper_limit) + ": ")) while guess < lower_limit or guess > upper_limit: print("That wasn't a valid guess. Try again...") guess = int(input("Enter an integer between " + str(lower_limit) + " and " + str(upper_limit) + ": ")) count = 1 # While loop that runs as long as the user is incorrect while guess != target: # Report whether the user's guess was too high or too low if guess > target: print("That's too high!") upper_limit = guess - 1 if guess < target: print("That's too low!") lower_limit = guess + 1 # Count the guess count = count + 1 # Read another guess from the user guess = int(input("Enter an integer between " + str(lower_limit) + " and " + str(upper_limit) + ": ")) while guess < lower_limit or guess > upper_limit: print("That wasn't a valid guess. Try again...") guess = int(input("Enter an integer between " + str(lower_limit) + " and " + str(upper_limit) + ": ")) # Report that the user has found the target number and the number of guesses # needed to find it print("That's correct! And it only took you", count, "guesses!")