ST EK List:
2.2.2A Software is developed using multiple levels of abstractions, such as constants, expressions, statements, procedures, and libraries.
5.2.1C Program instructions may involve variables that are initialized and updated, read, and written.

Developing a Number Guessing Game with Script Variables

Ruthless Suggestions from Al

In this lab, you will use two kinds of variables to store information:

On this page, you will develop a number guessing game that uses a script variable to keep track of a secret number.

Using a Script Variable (a Local Variable)

You have created variables as inputs to blocks that you made:
block definition for pinwheel and setting 'number of branches' input variable for pinwheel

You have also used the variable that the for block gave you:
local variable created by for block

Input variables and for variables are kinds of local variables; they work only within the script in which they're created. If you drag one into a different script, it won't work. Sometimes you need to create more variables to hold information temporarily while your script is running. The script variables block lets you do that.
script variables

For example, imagine a number-guessing game in which the player tries to guess the computer's secret number. The computer needs to store the secret number so that it can compare the player's guesses to it, but once the game is over, the secret number is no longer needed. If you play the game again, you want a new secret number.

: Local Variable

A variable that can be set or used only in the environment in which it is defined. This is the usual computer science term for variables that are defined as inputs to procedures or (in Snap! and some other languages) created by for or script variables.

  1. "U2L1-NumberGuessing"Create a new project called U2L1-NumberGuessing
  2. Create a new command block called Number guessing game.
  3. Inside this block, create a script variable called secret number (instructions below), and set it to a random number between 1 and 10.

    Making a Script Variable

    1. Move a script variables block into the Scripting Area. You can find it in the Variables palette.
      script variables
    2. Name the variable by clicking on the orange a at the end and typing the name you want. Here, it should be called secret number.
    Later, you will use the script variable by dragging it out of the script variables block, the way you drag an input variable, and placing it where you need it in your code.
  4. : Initialize

    Setting the initial value of a variable is known as initializing the variable.

  5. Use set to set the initial value of the variable. The down triangle lets you select the variable you want. Create code that sets secret number to a number randomly chosen from 1 through 10.
    set block menu selecting secret number
  6. The variable secret number is available in the set block only when you snap it into the same script that has the script variables block.

Checking the Player's Guess

You learned about loops in Unit 1, Looping with a Counter.

You'll want the computer to ask players to guess again and again until they get it right. To do that, you will use the repeat until block. Repeat until is a loop (just like repeat, forever, and for) that repeats until a certain condition is met. In this case, the code should repeat until the player's answer equals the secret number.

Repeat until is a conditional block (like if). Conditionals make choices based on a condition in the form of a Boolean value (either true or false). The condition is checked by a predicate, a true/false question such as a > 7. Predicates always report Boolean values. And they fit into hexagonal input slots, such as in if block and repeat until. Predicates help conditionals decide when to do something.

: Boolean
A Boolean value is either True or False. The word Boolean is capitalized because it's named after a person, George Boole, who invented the branch of mathematics dealing with Boolean operations (such as and, or, and not).
Pair Programming Swap
  1. Use repeat until to ask the player to guess the secret number until their answer equals the secret number.
    • Drag the secret number variable out of the script variables block to use it.
    • The ask () and wait and answer blocks go together. If you use ask to ask a question, the user's answer will be reported by answer.
  2. After the player guesses the secret number, make the computer congratulate the player.
  3. Test and debug. Take turns playing the game, and fix any problems with the code before moving on.
Checking that these pieces of your program work correctly before you go on will help make sure that you have a correctly working program at the end.
  1. When the computer congratulates the player for guessing correctly, have the computer say the number. For example, it might say, "You guessed it! My secret number was 7."
  2. Use join to merge the text "You guessed it! My secret number was" with the value of the secret number variable.