10.07.2017 |

Episode #5 of the course Coding сoncepts with Scratch by Alan Cohen


Coding is a form of communication between you, the coder, and the users of your program. Part of this communication is having a dialogue with your users, requesting necessary information, and then using that information in a meaningful way, based on what your program does.

This lesson introduces you to variables, which allow you to get information from users and do something with it.


Creating Variables

1. Click Data to display the Data blocks.

2. Click the Make a Variable button.

3. Enter a name for your variable and click the OK button.

Variables can store text, numbers, and special characters—for example, $, #, etc.

There are many things you can do with a variable. You can change its value, display the value, manipulate its value, etc.


Displaying the Variable

To show the value of a variable on the stage, tick the checkbox to the left of the variable name.

This is useful if you code a game and want the user’s current score to display, or to show any other information pertinent to the user.

Untick the checkbox to hide the value of the variable.

Changing the Value of a Variable

Use either the Set Variable To or the Change Variable By blocks to alter the value of a variable. The Set Variable To block allows you to set the value of the variable to a particular value. Use this block, for example, to reset the user’s score to zero in a game.

The Change Variable By block allows you to implement a variable by a set value. For example, you may have an educational program that awards the user five points for every correct answer and subtracts five points for every wrong answer.


Show Variable/Hide Variable Blocks

The Show Variable and Hide Variable blocks perform the same functions as ticking and unticking the checkbox next to a variable name. The difference is that your code determines when to show and hide a variable. These blocks are useful when you need to show and hide a variety of variables.


Using Text in a Variable

Variables can contain text. For example, you may want to use variables to hold the name, address, and phone number of a friend. You can get this information by coding a program that asks for this information.

The program uses the Ask block to get the necessary information. The Ask block, as you previously learned, stores the user’s response in the Answer variable. Since the Ask statement is used more than once, the program transfers the value from the Answer variable to other variables.

Here is the complete program.

So, you’ve already learned how to create, display, and hide variables and how to display and change the value of a variable. In the next lesson, you will learn about arithmetic and logic operators.


Recommended book

Coding Projects in Scratch by Jon Woodcock


Share with friends