import random # # constants, specifying the range in which the random number is picked # LOW = 1 HIGH = 100 # # play the game until the user says to stop # play = "yes" while (play == "yes"): # # Pick a random target value that the user is trying to guess # target = random.randrange(LOW, HIGH+1) guess = target + 1 num_guesses = 0 min_accepted = 1 max_accepted = 100 # # Loop until the user has guessed the correct number # while (guess != target): # # Read the guess from the user # guess = input("Enter a number between " + str(min_accepted) + " and " + str(max_accepted) + ": ") while (guess < min_accepted) or (guess > max_accepted): print "That wasn't in the valid range." guess = input("Enter a number between " + str(min_accepted) + " and " + str(max_accepted) + ": ") num_guesses = num_guesses + 1 # # Advise the user if the guess was too high, too low, or just right # if (guess < target): print "That was too low!" min_accepted = guess + 1 if (guess > target): print "That was too high!" max_accepted = guess - 1 print "It took you",num_guesses,"guesses!" # # Ask the user whether he/she wants to play again # play = raw_input("Do you want to play again (yes or no)? ")