# # Play a number guessing game with the user. The computer will choose the # target value, and the user needs to figure out what it is with as few # guesses as possible. # import random # Establish the range that will be used for the game. low = 1 high = 100 # Choose the target value that the user is trying to guess. target = random.randrange(low, high + 1) # Read a guess from the user. guess = int(input(f"Enter a guess between {low} and {high}: ")) count = 1 # Ensure that the value guessed by the user is within the valid range. while guess < low or guess > high: print("That wasn't a valid guess.") guess = int(input(f"Enter a guess between {low} and {high}: ")) # While the user has not guessed the target value... while guess != target: # Report whether the user's guess is too high or too low and count the guess if guess < target: print("Nope. That guess was too low.") low = guess + 1 elif guess > target: print("Too bad. That guess was too high.") high = guess - 1 count = count + 1 # Read a guess from the user. guess = int(input(f"Enter a guess between {low} and {high}: ")) # Ensure that the value guessed by the user is within the valid range. while guess < low or guess > high: print("That wasn't a valid guess.") guess = int(input(f"Enter a guess between {low} and {high}: ")) # Report that the user identified the correct value and the number of guesses # needed print(f"That's correct! And it only took you {count} guesses!")