Tip:
Highlight text to annotate it
X
In this sample, I have re-written the guessing game to include
looping. This will allow us to check and see if
the user has guessed a value that is in the right range
and it will also allow us to keep asking the user
to guess a number, until they guess correctly.
Like last time, we start with the integer, answer
which is going to be a random number between one and 100.
This is the number that we need to guess. Our other variable,
is our integer, guess, which we will get from the user.
Now, I have also included a
while statement, checking to see if either guess
is less than one, or if guess is greater than 100.
If either of those conditions are true, that means that
the user has guessed a number that is out of range. Let’s test that.
So I am going to enter a number that is over 100
Ahhh...I have a bug. What I haven’t done
is, I also need the child to
ask them to re-enter a number. Otherwise it
is stuck in an infinite loop, because there is no way for the condition to change.
generally, that is a common error, where you will
forget to make the condition change, so let’s try it again.
So I am going to put in something over
100, and now it asks me to
enter another number. We will try a negative number this time.
Still out of range.
It will prompt me to enter another number. This time I will enter 78, which should be in range.
And it told me that the guess was too low, and I was
cold. Well, that’s pretty high. Let’s try 90, and
see where we are at then. Too low, but hot.
93 -
95
97
99
must be 98.
so the program works.
and you can see that it checked to see if I was in range.
Now the other loop, which we have already seen demonstrated, is checking to see if
guess and answer are not equal, and as long as
guess does not equal the answer, each time we go through the loop
it is going to compare the two. If the guess is less than answer
that means they guessed too low, and
answer - guess is less than eleven, it will tell the user
that their hot - they are within 10 of the actual number.
And then it will get the integer from the user.
The other options are: if guess
is less than answer, and it is more than ten away
if guess is greater than answer, and more than ten away
or, the default, guess is greater than answer and more than ten away
and it will tell you your guess is too low, but you are hot
your guess is too low but you are cold, your guess is to high
but you are hot, or your guess is too high and you are cold. You can keep
guessing until you have gotten the answer correct, which will let you
leave the while loop.
And the child will say “Great job, the answer is” and your guess.
We will run it one more time. We will go directly to following the directions, and I am going to say 50
and generally, I would keep splitting
there, too low, but I am hot. So I am somewhere between 12 and 25.
So I’m going to try 18.
Too high, but hot
let’s try 16. Too high but hot.
15, too high but hot
14, too high but hot
13 - Great job! The answer is 13. So you can see that
the guessing game program is working the way that I intended.