Debugging and Extending Your Number Guessing Game

On this page, you will practice debugging, and you will make the computer give players hints.

  1. Click here to load this file. Then save it to your Snap! account. In the file is a definition of a number guessing game block. It 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.

    Talk with Another Pair Analyze the code, explain how this can happen, fix the bug, and save the project.

  2. Have it pick its random number just from 1 to 3 to make the bug occur more often.

    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

  3. Now reopen your U2L1-NumberGuessing project. Make sure your version doesn't have the same bug.

Jerry Lawson (1940-2011) was an American electronic engineer and gaming pioneer who is dubbed the "father of the video game cartridge." Born in New York City, Jerry took classes at both Queens College and City College of New York. While working as a Chief Hardware Engineer at Fairchild Semiconductor, he led the development of the Channel F console and pioneered the technology of the video game cartridge, which was popularized with the Atari 2600.

Article: One of the most important Silicon Valley pioneers you've never heard of

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.

The script variable secret number exists only until the script finishes running. When the game is played again, the program will create a new secret number variable and give it a new random number.

Save your work

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