A “Re-Worked” Number Guesser

In a previous project, my Computer Science 1 students were assigned the task of creating a “number guesser” in Python that could “guess” the number selected by the user based upon the user answering if the guess was too high or too low. To accomplish this task, we created a program similar to the following:

#Programmer Name: Eric Evans
#Program Name: Number Guesser
#Program Description: A program that will "guess" the number selected by the user that is between 1 and 100 using a series of questions.
runAgain = "Y"
high = 100
low = 1
guess = (high + low) / 2
#guess = random.randint(low, high)
while (runAgain =="Y"):
  correct = "N"
  guessCount = 1
  print("Let's play a guessing game")
  print("Pick a whole number between 1 and 100")
  print("You have to tell me if I get it correct or if I'm too high or too low.")
  print("Let's get started.")
  print("")
  while (correct == "N"):
    guess = int(guess)
    guessAsString = str(guess)
    print("Is Your Number " + guessAsString + "?")
    response = input("[C]orrect / [L]ow / [H]igh")
    response = response.upper()
    if (response == "C"):
      guessCountAsString = str(guessCount)
      print("Yay! I got it in " + guessCountAsString + " guesses!")
      correct = "Y"
    elif (response == "L"):
      print("Too Low Huh?")
      low = guess + 1
      guess = (high + low) / 2
      guessCount = guessCount + 1
    elif (response == "H"):
      print("Too High Huh?")
      high = guess - 1
      guess = (high + low) / 2
      guessCount = guessCount + 1
  print("Would You Like to Run the Program Again?")
  runAgain = input("[Y]es or [N]o")
  runAgain = runAgain.upper()
print("Thank you for playing.")
print("Exiting Program - Goodbye")

Now, for the second version of this program, the roles will be flipped. The computer will now be selecting a whole number between 1 and 100 and the user will be attempting to guess the number.

Here is an basic example of what a possible solution could look like:

#Programmer Name: Eric Evans
#Program Name: Number Guesser - Take 2
#Program Description: A program that will select a random number and have the user attempt to guess the chosen number.
import random
#Imports the random library to allow for random number generation
runAgain = "Y"
#Declares and initializes the variable runAgain with a value of "Y" 
high = 100
#Declares and initializes the variable high with a value of "100"
low = 1
#Declares and initializes the variable low with a value of "1"
while (runAgain =="Y"):
#Starts a while loop to allow the program to run again.
  computerNumber = random.randint(low, high)
  #Declares and initializes the variable computerNumber with a randomly selected integer between the value of the variables high and low.
  correct = "N"
  #Declares and initializes the variable correct with a value of "N"
  guessCount = 1
  #Declares and initializes the variable guessCount with a value of "1"
  print("Let's play a guessing game")
  #Outputs information to the screen for the user
  print("I am going to pick a whole number between 1 and 100")
  #Outputs information to the screen for the user
  print("After each guess, I will tell you if you are correct or if you're too high or too low.")
  #Outputs information to the screen for the user
  print("Let's get started.")
  #Outputs information to the screen for the user
  print("")
  #Visually breaks-up the information on the screen for the user
  while (correct == "N"):
    #Starts a nested while loop to allow the user to make more guesses until the variable correct is equal to "Y"
    guess = input("What Is Your Guess? ")
    #Declares and initializes variable guess with the value entered by the user from the keyboard
    guess = int(guess)
    #Casts the variable guess as an integer
    if (guess == computerNumber):
    #Starts a conditional if statement to address when the user guesses the correct number
      guessCountAsString = str(guessCount)
      #Declares and initializes variable guessCountAsString with the value of the variable guessCount cast as a string
      print("Wow! You guessed my number in " + guessCountAsString + " guesses!")
      #Outputs information to the screen for the user
      correct = "Y" #Sets the value of the variable correct to "Y" to exit the inner while loop
    elif (guess < computerNumber and (abs(computerNumber - guess) > (int(computerNumber * .15)))):
    #Starts a conditional else/if statement to address when the user guesses a number that is less than the number selected by the computer and is more than 15% of the value of the computerNumber away
      guessAsString = str(guess)
      #Declares and initializes variable guessAsString with the value of the variable guess cast as a string
      print("Your guess of "+ guessAsString + " is too low.")
      #Outputs information to the screen for the user
      print("Please try again.")
      #Outputs information to the screen for the user
      print("")
      #Visually break-up the information on the screen for the user between their guesses
      guessCount = guessCount + 1
      #Increments the value of the guessCount variable by 1
    elif (guess < computerNumber and (abs(computerNumber - guess) <= (int(computerNumber * .15)))):
    #Starts a conditional else/if statement to address when the user guesses a number that is less than the number selected by the computer and is less than or equal to 15% of the value of the computerNumber away
      guessAsString = str(guess)
      #Declares and initializes variable guessAsString with the value of the variable guess cast as a string
      print("Your guess of "+ guessAsString + " is too low.")
      #Outputs information to the screen for the user
      print("You are very close!!!")
      #Outputs information to the screen for the user
      print("Please try again.")
      #Outputs information to the screen for the user
      print("")
      #Visually break-up the information on the screen for the user between their guesses
      guessCount = guessCount + 1
      #Increments the value of the guessCount variable by 1
    elif (guess > computerNumber and (abs(computerNumber - guess) > (int(computerNumber * .15)))):
    #Starts a conditional else/if statement to address when the user guesses a number that is greater than the number selected by the computer and is more than 15% of the value of the computerNumber away
      guessAsString = str(guess)
      #Declares and initializes variable guessAsString with the value of the variable guess cast as a string
      print("Your guess of "+ guessAsString + " is too high.")
      #Outputs information to the screen for the user
      print("Please try again.")
      #Outputs information to the screen for the user
      print("")
      #Visually break-up the information on the screen for the user between their guesses
      guessCount = guessCount + 1
      #Increments the value of the guessCount variable by 1
    elif (guess > computerNumber and (abs(computerNumber - guess) <= (int(computerNumber * .15)))):
    #Starts a conditional else/if statement to address when the user guesses a number that is greater than the number selected by the computer and is less than or equal to 15% of the value of the computerNumber away
      guessAsString = str(guess)
      #Declares and initializes variable guessAsString with the value of the variable guess cast as a string
      print("Your guess of "+ guessAsString + " is too high.")
      #Outputs information to the screen for the user
      print("You are very close!!!")
      #Outputs information to the screen for the user
      print("Please try again.")
      #Outputs information to the screen for the user
      print("")
      #Visually break-up the information on the screen for the user between their guesses
      guessCount = guessCount + 1
      #Increments the value of the guessCount variable by 1
  print("Would You Like to Run the Program Again?")
  #Outputs information to the screen for the user
  runAgain = input("[Y]es or [N]o")
  #Sets the value of the variable runAgain to the keyboard input received to allow user to repeat program or exit the outer-loop
  runAgain = runAgain.upper()
  #Sets the value of the variable runAgain to upper-case
print("Thank you for playing.")
#Outputs information to the screen for the user
print("Exiting Program - Goodbye")
#Outputs information to the screen for the user

As you can see, this version of the program also provides “encouragement” as you close-in on the selected number. Once you are within 15% of that number on the number-line you are told that you are very close.

Leave a Reply

Your email address will not be published. Required fields are marked *