## 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
#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
#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
#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
#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.

## Projected Grades – 6 Weeks to Go

We are now in the final 6-week stretch of the 2017/2018 school year! I have been running various projections for end-of-semester and end-of-year grades.

#### Principles of Applied Engineering

With a total of 46 students enrolled in PAE, I can handle a failure rate of 4.35%. I not happy with that number as there is no excuse for failure of this class beyond simply not turning in assignments. Hopefully, these failing students will pull-up their grades in the final 6-weeks of instruction and be able to pass for the year.

#### Computer Science 1

As you can see, my 4th Period A Day class has all A’s and B’s in the class. They have an overall class average of 93.59. In contrast, my 2nd Period A Day class has an overall class average of 85.15.

There are a total of 28 students currently enrolled in CS1. I am not satisfied with a failure rate of 7.1%. There is absolutely no excuse for this rate beyond students not submitting work. As with PAE, I am hopeful that the students pull their grades up and are able to pass for the year.

## Python Number Guesser

Last year, I gave a problem in JAVA to create a number guesser. The idea was that the computer would randomly select a number and the user had to guess the number the computer had picked. http://funmultiplies.com/semester-review-computer-science

This year, in Computer Science 1, we have done a similar assignment in Python. However, in this version, the user is selecting the number and the computer has to “guess” the number the user selected.

On the first day of this project, students had to first choose a solving strategy. The two best strategies were called Hi/Lo Average and Hi/Lo Random.

The idea with both of these is that we ask the user to pick an integer (whole number) between 1 and 100.

With Hi/Lo Average, we take the average of the highest and lowest numbers, in this case 100 and 1 and we get 49. So, our first guess will be 49. If that is too low, then we move the lowest value up from 1 to 48. If 49 was too high, then we would move the highest value down from 100 to 50. We then make our second guess by taking the average of the new highest and lowest numbers. We repeat this process until we get to the number the user selected. The key advantage to this process is that we are guaranteed to solve the problem in 7 or fewer questions.

With Hi/Lo Random, we move the highest and lowest numbers identically to how we did in Hi/Lo Average after each incorrect guess, but we don’t take the average – we just select a random number between the highest and lowest numbers. This can potentially solve the problem very quickly or take considerably longer than Hi/Lo Average since you are entirely dependent upon what integers are selected by a random number generator.

Here is what a possible Hi/Lo Average solution looks like:

```#Programmer Name: Eric Evans, M.Ed.
#Program Name: Number Guesser (Hi/Lo Average)
#Program Description: Uses a Hi/Lo Average algorithm to determine the number chosen the user.
#Algorithm: Starts with hi/lo or 100/1. Calculates average of hi/lo. Adjusts hi/lo accordingly after guess. Repeat calculation of average between new hi/lo. Repeat hi/lo adjustment...
#
correct = "N"
#Creates a variable named "correct" and sets the initial value as N
high = 100
#Creates a variable named "high" and sets the initial value as 100
low = 1
#Creates a variable named "low" and sets the initial value as 1
guessCount = 1
#Creates a variable named "guessCount" and sets the initial value as 1
guess = (high + low) / 2
#Creates a variable named "guess" and sets the initial value as the average of high and low
while (correct == "N"):
#Starts a loop that runs as long as the variable "correct" has a value of "N".
guess = int(guess)
#Casts the value of the variable "guess" as an integer. This truncates any decimals.
guessAsString = str(guess)
#Creates a variable named "guessAsString" and sets the initial value as the value of the variable named "guess" as a string.
print ("Is Your Number " + guessAsString + "?")
#Outputs the current "guess" for the user to see.
response = input("[C]orrect  /  Too [L]ow  /  Too [H]igh")
#Creates a variable named "response" and sets the initial value as the input from the keyboard.
if (response == "C"):
#Start of a conditional statement if the keyboard input is "C".
guessCountAsString = str(guessCount)
#Creates a variable named "guessCountAsString" and sets the initial value as the value of the variable named "guessCount" as a string.
print("Yay! I got it in " + guessCountAsString + " guesses!")
#Outputs the line displayed when the correct number is guessed.
correct = "Y"
#Changes the value of the variable named "correct" to "Y".
elif (response == "L"):
#Start of a conditional statement if the keyboard input is "L".
print ("Too Low Huh?")
#Outputs an acknowledgement to the user of their input.
low = guess + 1
#Changes the value of the variable named "low" to the value of the current guess + 1.
guess = (high + low) / 2
#Changes the value of the variable named "guess" to the value of the average of high and low.
guessCount = guessCount + 1
#Changes the value of the variable named "guessCount" by adding 1 to its existing value.
elif (response == "H"):
#Start of a conditional statement if the keyboard inputs is "H".
print ("Too High Huh?")
#Outputs an acknowledgement to the user of their input.
high = guess - 1
#Changes the value of the variable named "high" to the value of the current guess - 1.
guess = (high + low) / 2
#Changes the value of the variable named "guess" to the value of the average of high and low.
guessCount = guessCount + 1
#Changes the value of the variable named "guessCount" by adding 1 to its existing value.
print ("Exiting Program")
#Outputs a message letting the user they are exiting the program.```

At the time of the first writing, I omitted the Hi/Lo Random solution as I wanted to see if my students could identify a key weakness inherent in that strategy. Here is a possible Hi/Lo Random solution (I discuss the weakness below):

```#Programmer Name: Eric Evans, M.Ed.
#Program Name: Number Guesser (Hi/Lo Random)
#Program Description: Uses a Hi/Lo Random algorithm to determine the number chosen the user.
#Algorithm: Starts with hi/lo or 100/1. Selects a random number between hi/lo. Adjusts hi/lo accordingly after guess. Repeat selection of a random number between new hi/lo. Repeat hi/lo adjustment...
#
import random
correct = "N"
high = 100
low = 1
guessCount = 1
guess = random.randint(low, high)
while (correct == "N"):
guess = int(guess)
guessAsString = str(guess)
print ("Is Your Number " + guessAsString + "?")
response = input("[C]orrect  /  Too [L]ow  /  Too [H]igh")
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 = random.randint(low, high)
guessCount = guessCount + 1
elif (response == "H"):
print ("Too High Huh?")
high = guess - 1
guess = random.randint(low, high)
guessCount = guessCount + 1
print ("Exiting Program")```

If you played the Hi/Lo Random version of the game, you realized that there is nothing in the program to prevent the computer from randomly guessing the same number multiple times. This would need to be addressed by using a data structure to store the guesses and compare them as each subsequent guess was being made.

In addition, the odds are not “favorable” with a Hi/Lo Random game when compared to a Hi/Lo Average game.

Let’s say that the user has selected the number 21 as their number. Now, let’s look at the odds through each run compared side-by-side. I am going to use the random number generator from Google to generate the guesses for this example:

 Run # Hi/Lo Average Hi/Lo Random Guess Odds Guess Odds 1 50 1:100 16 1:100 2 25 1:49 48 1:30 3 12 1:23 17 1:29 4 18 1:11 27 1:8 5 21 1:1 25 1:6 6 N/A N/A 21 1:1

As you can see, with Hi/Lo Random, you are completely at the mercy of the numbers that are randomly generated whereas with Hi/Lo Average, you are guaranteed that your odds are twice as good after each guess.

## Lesson Plans – Week of 2018-04-01

#### Lesson Plans – Week of 2018-04-01

• (1 A/B) Principles of Applied Engineering
• (2A) Computer Science 1
• (2B) Computer Science 2
• (3A) Robotics 1 / Robotics 2 / FTC 11242
• (3B) Robotics 1 / Robotics 2 / FTC 12645
• (4A) Computer Science 1
• (5 A/B) Principles of Applied Engineering

Eric Evans – Planboard Week – 2018-04-01

## Lesson Plans – Week of 2018-03-25

#### Lesson Plans – Week of 2018-03-25

• (1 A/B) Principles of Applied Engineering
• (2A) Computer Science 1
• (2B) Computer Science 2
• (3A) Robotics 1 / Robotics 2 / FTC 11242
• (3B) Robotics 1 / Robotics 2 / FTC 12645
• (4A) Computer Science 1
• (5 A/B) Principles of Applied Engineering

Eric Evans – Planboard Week – 2018-03-25