The Guessing Game

Debugging the Design

I think the obvious place to insert the variable line is right here where you need it:

iterate
Variable middle = 50
Say "Is your number less than middle"

There are several problems with this program. You could try it in the Kitchen computer, but you have not yet said when or how the iteration ends, so it keeps going around and around, asking the same question. Click the red stop sign to stop, then click the "Go Back" button there to come back here.

God gets His creation perfect on the first cut, the rest of us make mistakes and spend a lot of time fixing those mistakes. These are easy, we can see what went wrong. If your program is anything like mine, the first thing you probably noticed is that some of the text ran off the right edge. You just need to make the lines shorter. I think you can figure out how to do that.

Then when it came time to ask the user if their number is less than 50, it didn't say the number 50, but instead it printed the variable name. Do you know why it did that? Everything inside the quotes is (like the real English language) exactly what you want it to say. When you use a variable, it needs to be outside the quotes. So the question we should be asking is:

Say "Is your number less than " middle "?"
Notice that there are no quotes around the variable name, it's the name of a value (in this case 50) not the value itself. When you refer to a variable by its name (not inside quotes), the computer assumes you want its value. Also notice there's a space between the word "than" and the closing quotation mark. If you leave that space off then you get the text run-on to the number 50, like "than50". Maybe a human reading it would figure out what you meant, but they might wonder. Fortunately, that kind of mistake is easy to see and fix.

Steve has a joke he likes to tell about engineers, this engineer went to take a shower and wash his hair, and didn't come back out. So somebody went in to see what happened, and he was reading the instructions on the bottle like all good engineers do, and it said "Wash, rinse, repeat." He was still repeating. Only an engineer. The rest of us know that it means "repeat once" but the instructions didn't say that. Computers are like that engineer, you must tell them explicitly what you want.

So how many times do we want it to repeat? I happen to know that for 99 numbers, seven times is sufficient, but we can do better than that. We can tell the computer to exit the loop (loop is another word for iteration or repeat) after the range of numbers is down to one -- that would be the number they were thinking of. So how do we know what the range of numbers is? Initially we announced it would be from 1 to 99. After they answer the first question, the range will be cut in half, and one of those ends is going to move (it will vary). What does that suggest to you about how you are going to handle these end points? Think about how you are going to do it before you turn the page.

Five Basic Concepts
Sequence
Iteration
Conditional
Variables
Input/Output

 

<<Previous | ToC | Next >>

[2021 June 3]

His Only a guy engineer is sufficiently OCD to do this, so the male pronoun in this context is completely accurate.