3
Page 1: Starting a Number Guessing Game
Unit 2, Lab 1, Page 1
On this page, you will begin to develop a number guessing game that uses a local variable to keep track of a secret number.
- “U2L1-NumberGuessing”
- 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.
: Variable
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.
Create a script variable called secret number to store the number that the player will try to guess (instructions below).
Making a Script Variable
-
Move a
script variables
block into the Scripting Area. You can find it in the Variables palette.
- 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.-
Move a
Use 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 thescript variables
block.
secretNumber ← 7
or .
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.
You have created variables as inputs to blocks that you made:
You have used the counter variable that the for
block gave you:
: 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.
You learned about input variables on Unit 1 Lab 3 Page 3: Blocks with Inputs.
a ← a * 2means 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
.
-
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)
Correct! The variable b is assigned the value of the variable a (which is 3 when
b ← a
is done).
4
The variable b is given the value of the variable a (which is 3 when
b ← a
is done); b doesn’t remember that the 3 came from a so it doesn’t change when a is changed.
a
The variable b has been set to the value of the variable a, not the letter “a” itself.
b
The script will display the value of the variable b, not the letter “b” itself.
-
What value will this script report?
-5
Correct!
3
The value of the variable m is always greater than the value of the variable k in this script, so subtracting m from k will give a negative number.
-4
The value of the variable m is 9 at the beginning, but it has been changed by the time it is subtracted from k.
5
The last
set
command sets the value of the variable k to the value k – m, not m – k.