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.

  1. 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.
    Number guessing game{script variables(secret number); set(secret number) to (pick random(1) to (10)); repeat until(answer=secret number){ask(Guess my secret number) and wait} say(You guessed it!) for (2) secs}
    Talk with Another Pair Analyze the code, or experiment with it, and explain how this can happen.
  2. If it isn't open already, open your U2L1-NumberGuessing project.
  3. 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

    One way uses a second script variable to store and check the answer. You might find another way that you like better.
    If you want to add more script variables, use the right-pointing triangle. (The left triangle will remove them.)
    clicking triangles to add and remove script variables

    Debugging Tip: Display of Variables

    When you are debugging your code, it can help to see the value of a variable at different points in your program. There are several ways to do this:

    • You can click the variable block (such assecret number) occasionally to report its current value.
    • If it's a global variable, you can mark the check-box beside it in the palette (checkbox next to secret number) to have a watcher display on the stage. (This also works for some reporters that aren't variables, as shown in the animation below.)
    • If it's a local (script) variable, you can use the show variable block to display a watcher on the stage (and use hide variable to hide it again). (This is also shown in the following animation.)

    Animation showing how to display answer and script variables
  4. Test and debug your code.

Extending the Program with More Specific Responses

  1. 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."
    Talk with Your Partner Where in the code should these conditionals go?
  2. 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.

Now Is a Good Time to Save

In a later unit, you will teach the computer how to guess a secret number that you have chosen.

  1. 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.
 
  1. 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.
  2. 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."