Empezando un Juego para Adivinar un Número

En este laboratorio, tú vas a usar variables locales y globales para almacenar información.

En esta página, tú vas a empezar a desarrollar un juego para adivinar un número que usa variables locales para llevar el registro del número secreto.

  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 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'.
In the language on the exam, the ← operator actually makes a copy of the value it's assigning. So, for example, the statement
secretWord ← "supercalifragilisticexpialidocious"
allocates memory for "supercalifragilisticexpialidocious" again; the word was already stored in the code statement, and now a copy is stored in the variable
secretWord
. This is not the case in Snap! and most other languages, where there is only one copy that the variable and the code share, which is a more efficient way for programming languages to operate.
This isn't true. First, the whole issue about copying arises only for mutable data, so really we should talk about this with respect to lists. Second, Snap! actually does make a copy in the case of a literal list in the code, because if the program mutates the list that's been assigned to the variable, the programmer probably does not want the code itself to be changed! If you assign to a variable the list-valued result of some expression other than just calling list with constant items, that's when we don't make a copy. This is too hairy for me to fix tonight.

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.

Show me examples I'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. What value will this code display?
    a ← 3
    b ← a
    a ← 4
    DISPLAY(b)
    3
    4
    a
    b
    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