Starting a Number Guessing Game

June said page was hard to follow in PD. (Too much interjection.) Folks have to attach SET to SCRIPT VARIABLES in order to get SECRET NUMBER to show up in SET. --MF, 8/1/23

In this lab, you will use local and global variables to store information.

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

  1. "U2L1-NumberGuessing"Create a new project called U2L1-NumberGuessing
  2. Start building a new command block called number guessing game that will contain the code for the game. Leave the Block Editor open.

In a number-guessing game, the player tries to guess the computer's secret number. The computer needs a way to store the secret number in a variable so that it can compare it to the player's guesses.
script variables (secret number)

: Variable
AAP-1.A.1

A variable is like a labeled box that can hold one value at a time, such as one word, one costume, or one list (which can contain many things). You can look at what's inside as many times as you want.

On Unit 1 Lab 2 Page 2: Making Programs Talk, you learned about procedural abstraction: giving scripts names by putting them in new blocks. Here, we are starting to look at data abstraction, giving names to numbers, text, lists, etc. When you give something a name, you can refer to it without knowing exactly what the value is.
  1. Create a script variable called secret number to store the number that the player will try to guess (instructions below).

    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) and placing it where you need it in your code.
  2. AAP-1.A, AAP-3.E
    Use set () to () to set the initial value of secret number to a random number from 1 to 10. The set menu lets you select which variable to set.
    The variable secret number is available in the set block only when you snap it somewhere after the script variables block.
    set block menu selecting secret number
AAP-1.B.2
set (secret number) to (7) would be written as
secretNumber ← 7
or a white rounded rectangle containing the text 'secretNumber ← 7'.

Script variables are a kind of local variable; they work only within the script where they're created. If you drag one into a different script, it won't work. You've seen two kinds of local variables before: inputs to blocks and for counters.

Examples you've seen before.

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 used the counter variable that the for block gave you:
local variable created by for block

: Local Variable

A local variable can be set or used only in the environment in which it is defined. This term includes inputs to procedures and variables created by the for or script variables block.

In algebra, a variable is sometimes used for something whose value you don't know yet, and the goal is to find out its value. In programming you decide the values of variables.

AAP-1.A.1
You learned about input variables on Unit 1 Lab 3 Page 3: Blocks with Inputs.
When you assign a value to a variable, the variable holds that value, not where it came from. For example, if apples = 2 and you set (bananas) to (apples), then bananas will hold the value 2 and have no memory of it having come from apples. This is why
a ← a * 2
means something. (Suppose a = 8. First compute the value of
a * 2
, namely 16, and then replace the old value of a with 16). Up to now, the only variables you've used are input variables, and you never assign a value to an input because the value is given by the code that calls it. But a script variable won't have a value until you give it one with set.
    AAP-1.B, AAP-1.B.3
  1. Examples like this one are useful to test your understanding of assignment to variables, but you wouldn't use a sequence of assignments like this in an actual program.
    What value will this code display?
    a ← 3
    b ← a
    a ← 4
    DISPLAY(b)
    3
    4
    a
    b
  2. What value will this script report?
    set m to 9, set k to 5, set m to (m + 1), set k to (k - m), report k
    -5
    3
    -4
    5