+21622886281
Scratch

Variables in Scratch

In Scratch, variables are like containers that you can use to store values, such as numbers or strings of text. You can use variables to keep track of important data in your program, and to make your program more flexible and interactive.

There are two types of variables in Scratch: global variables and local variables.

Global variables are variables that can be accessed and modified by any script in your program. They are useful for storing data that you want to use in multiple scripts or that you want to share between different sprites.

Local variables are variables that are only accessible and modifiable by the script in which they are defined. They are useful for storing data that is only needed in a particular script and that you don’t need to share with other scripts or sprites.

Here are some examples of how you can use variables in Scratch:

  1. Keeping track of the score in a game: You can create a global variable called « score » and use it to keep track of the player’s score as they progress through the game.
  2. Storing user input: You can use the « ask [question] and wait » block to ask the user a question and store their answer in a variable.
  3. Changing the appearance of a sprite: You can use a variable to store the size or color of a sprite and use it to change the sprite’s appearance.

Variables are a powerful tool in Scratch that can help you create more interactive and flexible programs.

How to use variables in Scratch?

1) A variable is declared by clicking on the variables category.

2) Then we click on create a variable.

3) Then we give a name to this variable.

To learn how to use variables in Scratch, we’ll create a program that calculates the sum of two numbers.

First, we want to create 3 variables a , b and somme to store the three numbers.

Then we assign to the two variables a and b two values ​​using these two instructions:

Next, we need to sum the variables a and b and store the result in the somme variable:

Here is the final program:

Finally here is the result of the program on the Scratch stage:

 

Télécharger le programme




0 commentaire


Leave a comment

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Scroll to Top