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.

Deques of Python Stories

I was introducing deques in my Computer Science 1 class today in Python and decided to tell a story around the lesson. I figured I would share as this seemed to sink-in with my classes.

Zara loves Computer Science and using technology. She is hosting a party with several of her friends and wants to have their names displayed on the screen in the order they arrived and then remove them when they depart.

The friends of Zara are:

  • Alice
  • Betty
  • Christy
  • Darla
  • Elizabeth
  • Fran
  • Ginger
  • Holly

Alice, Betty and Christy arrive at the party at the same time and enter in that order.

from collections import deque
myGuestList = deque(["Alice", "Betty", "Christy"])
print(myGuestList)

At this point, we have 3 guests at the party: Alice in index 0, Betty in index 1, and Christy in index 2.

Darla and Elizabeth arrive at the party at the same time and enter in that order.

myGuestList.append("Darla")
myGuestList.append("Elizabeth")
print(myGuestList)

We now have 5 guests at the party: Alice in index 0, Betty in index 1, Christy in index 2, Darla in index 3, and Elizabeth in index 4.

Alice and Betty H-A-T-E Elizabeth. Almost instantly upon seeing her name on the screen, they both leave the party.

myGuestList.popleft()
myGuestList.popleft()
print(myGuestList)

We now have 3 guests at the party: Christy has now moved to index 0, Darla has moved to index 1, and Elizabeth has moved to index 2.

Ginger and Holly arrive at the party at the same time and enter in that order.

myGuestList.append("Ginger")
myGuestList.append("Holly")
print(myGuestList)

We now have 5 guests at the party: Christy is in index 0, Darla is in index 1, Elizabeth is in index 2, Ginger is in index 3, and Holly is in index 4.

Regretfully, Holly had been to another party before coming to Zara’s party and gets sick when she gets inside and has to leave.

myGuestList.pop()
print(myGuestList)

We now have 4 guests at the party: Christy is in index 0, Darla is in index 1, Elizabeth is in index 2, and Ginger is in index 3.

Elizabeth is Holly’s sister and leaves to take care of her sister and possibly ridicule her for her choice of the day.

del myGuestList[2]
print(myGuestList)

We now have 3 guests at the party: Christy is in index 0, Darla is in index 1, and Ginger who has moved to index 2.

Zara realizes that while the computer shows 3 guests at the party, there are 4 guests there. Somehow, Fran came in right after Elizabeth and did not get entered into the computer during the “drama” between Alice and Betty with Elizabeth. Fran needs to be entered into the correct location.

myGuestList.insert(2,"Fran")
print(myGuestList)

We now have 4 guests at the party: Christy is in index 0, Darla is in index 1, Fran in index 2, and Ginger who has moved to index 3.

Zara realizes that there is one person who has been there the entire time, but is not on the list – herself. Since she has been there the entire time, she should be placed at the bottom of the deque.

myGuestList.appendleft("Zara")
print(myGuestList)

We now have 5 people at the party: Zara is in index 0, Christy moves index 1, Darla moves to index 2, Fran moves to index 3, and Ginger moves to index 4.

Everyone, including Zara agrees that the party is pretty lame and that there is a better party down the street. So, everyone is going to leave the party.

myGuestList.clear()
print(myGuestList)

We now have nobody at the party and the deque is empty!