3 minute read
Manipulating data
Programs are often used to manage and process data. This data is either provided by the user or collected from other computer systems. In Scratch, the Operator blocks are used to manipulate numbers and text stored in variables.
Variables
Advertisement
Many programming languages use variables to store information. A variable can store one piece of information, either text or a number. In a game, for example, two variables might be used to store the player’s name and score.
1Make a variable To create a variable in Scratch, select Variables in the Blocks Palette and click on the Make a Variable button. Give the new variable a meaningful name, such as score, so that the code is easy to understand. Usually, variables need to be created for all sprites, which means that all sprites can see and change the variable.
Make a Variable
my variable
score
The new variable will be displayed here
2 Use blocks with your variable Use the set [variable name] block to reset the variable’s value. For example, set score to 0. The change [variable name] by block can be used to increase or decrease the value.
set score to 0 change score by 1
Strings
Programmers often call a piece of text in a program a “string”. For example, a string can be a name, an answer to a question, or an entire sentence. In Scratch, any variable can store a number or a string, and it can store different values at different times.
join apple banana
Join strings
This block can be used to join two strings. The strings are joined without a space, so the result for this example will be “applebanana”. Variable blocks can also be used in place of words typed into the block.
letter 1 of apple
Extract letters
This block extracts one letter from a string. In this example, the first letter of the string “apple” is extracted.
length of apple
Count a string
The number of characters in a string can be counted using this block. The result for the block can be viewed by clicking on it. It can also be dropped into other blocks to use in a script.
apple contains a ?
Check strings
This block checks whether the second string input is in the first one, and gives the answer as true or false. It is also possible to check for more than one letter: apple contains app?
LISTS
A list is used to store similar pieces of information, such as a list of names. In Scratch, a list can be created from the Variables section of the Blocks Palette. List positions are used for inserting and deleting items. For example, the delete 2 of [list name] block can be used to remove the second item from a list.
Numbers
Operator blocks are a core part of Scratch, and can be used for arithmetic operations, comparisons, and to pick random numbers. Some operator blocks even work with strings.
Used for addition Used for subtraction
7 + 2 7 – 2
7 * 2 7 / 2
Computers use the symbol * for multiplication Computers use the symbol / for division
pick random 1 to 10
Chooses a random number between 1 and 10
when clicked
go to x: 0 y: 0 erase all
set length of sides to
set number of sides to
Length of the shape’s sides is randomly chosen, between 10 and 100 The angle to turn is set at 360 degrees divided by the number of sides
pick random 10 to 100
pick random 3 to 8
set angle to turn to 360 / number of sides
pen down
repeat number of sides
This repeats the code to draw each side
move length of sides steps
turn angle to turn degrees
pen up move 150 steps
Moves the sprite out of the way after drawing Drop the variable blocks into hole in the Motion block
Drawing random shapes The program above draws a random shape each time the green flag is clicked. Start by making the variables length of sides, number of sides, and angle to turn. Click the green flag several times to create random art.