ST EK List:
5.4.1 Evaluate the correctness of a program. [P4]
5.4.1E Locating and correcting errors in a program is called debugging the program.
5.4.1F Knowledge of what a program is supposed to do is required in order to find most program errors.
Debugging and Extending Your Number Guessing Game
On this page, you will debug a Number Guessing game script, and you will make the computer tell players whether their guesses are too big or too small.
-
Analyze and Debug. This definition for a
Number guessing game
block has a bug—an error in the code that makes the program behave differently than expected. This code, if you keep playing over and over, will mostly work, but sometimes (not often) it will congratulate you before you even guess.
This is a kind of debugging strategy (perhaps: "narrowing the possibilities"?). Do we want to say anything more about it? --MF, 3/21/18
If you build this to try it out, have it pick its random number just from 1 to 3 to make the bug occur more often.

Analyze the code, or experiment with it, and explain how this can happen.
- If it isn't open already, open your U2L1-NumberGuessing project.
-
There are a few different ways to fix the bug.
At first, I wondered if this might be something that should be included in the debugging tip, but then I realized that I'm not sure what it means. Can someone who understands the intended meaning please look at this and explain it to me so we can work to clarify it? --MF, 3/21/18
If you want to add more script variables, use the right-pointing triangle. (The left triangle will remove them.)
- Test and debug your code.
Extending the Program with More Specific Responses
- Use more conditional blocks and predicates to make the computer tell the player whether a guess is too big or too small: "That's too big. Try again."
Where in the code should these conditionals go?
-
Test and debug. Play your game enough to see if it works the way you want. It should:
- ask the player to guess its secret number;
- say whether a wrong guess is too big or too small, and then ask again;
- congratulate the player for a correct answer.
Script variables exist only while the script is running, so the secret number variable will not exist after Number guessing game
finishes running. When played again, the program will create a new secret number variable and initialize it with a random number.
In a later unit, you will teach the computer how to guess a secret number that you have chosen.
- Right now, the script always picks a number between 1 and 10. At the start of the game, ask the player what the maximum number should be, and make your program choose a number between 1 and maximum.
- Use another script variable to keep track of how many guesses the player makes before getting the right number. When the player guesses the secret number, say how many guesses it took.
- Find a way to vary the language a bit so that your program does not always repeat exactly the same words in the same situation. For example, if a player guesses too high twice in a row, the program could say "That's still too big. Try another number."