Sixth Change: Variables & List
Quick References for all Variables & List blocks
Make a Variable button: This button allows you to create a new variable. A variable is a place to store data that can change over time.
(variable name) (check box to show): This is an indicator for a variable. Check the box to display the variable's value on the stage.
set [variable name] to (number/text): This block sets the value of a variable to a number or text.
change [variable name] by (number): This block changes the current value of a variable by adding a number(can be negative) to it.
show variable [variable name]: This block makes the value of a variable visible on the stage.
hide variable [variable name]: This block hides a variable from the stage, making it invisible.
Make a List button: Similar to the "Make a Variable" button, this is button allows you to create a new list. Lists are used to store multiple values.Make a List button: This button allows you to create a new list, similar to the "Make a Variable" button. A list can hold multiple values that can be individually accessed and modified.
(list name) (check box to show): This checkbox allows you to toggle the visibility of the list on the stage. When checked, the list's contents will be shown.
add (thing) to [list]: This block adds an item (replace "thing" with your item) to the end of the specified list.
delete (1) of [list]: This block removes an item from a list at the specified index (e.g., the 1st item).
delete all of [list]: This block clears all the items from the list, leaving it empty.
insert (thing) at (1) of [list]: This block inserts an item into the list at the specified position (e.g., at index 1).
replace item (1) of [list] with (thing): This block replaces the item at a specific index with a new value.
item (1) of [list]: This block returns the value at the specified position (e.g., the 1st item) in the list.
item # of (thing) in [list]: This block returns the index of a specified item within the list.
length of [list]: This block returns the number of items currently in the list.
[list] contains (thing) ?: This block checks if the specified item exists in the list. It returns "true" if the item is found, otherwise it returns "false."
show list [list]: This block makes the list visible on the stage.
hide list [list]: This block hides the list from the stage.
These blocks are foundational for creating interactive projects in Scratch that require tracking and updating data, such as scores in a game, counters, or storing input from users. Variables and lists can be manipulated through these blocks to perform a variety of functions within a Scratch project.
Image Illustrate by ChatGPT
Value - An answer to Save
In programming, a value is a piece of information or data a computer uses. It can be a number, a word, or even a true/false statement. Think of a value like an important answer you calculated on scratch paper or in your head. We need to store values in variables or lists so the computer can remember and use them later. Just like how we use values in math or when keeping track of things, computers use values to do calculations, make decisions, or show information.
Variable - A storage box for Value
A variable in programming is like a container that holds values, such as a number or a word, that the computer can use. You can give the variable a name and change what's inside it whenever you need to, just like how you might store different items in a labeled box. Variables help the computer remember and work with the information during a program.
List - A backpack in the Game
A list in programming is like an inventory backpack in a video game. You can store multiple items in it similar to variables, such as numbers, words, or even another list. Just like in a game, you can add, remove, or check items in the list. Each item has its own spot number called index in programming. This helps the computer find exactly what it needs, as you pick items from your inventory slots.
set [variable name] to (number/text)
change [variable name] by (number)
Set block sets the variable to a value. We also call this step initialization.
Change vairble by block will add a number to the previous value in the variable (can only be a number). Change variable by 1 means add 1 to it, and change variable by -1 means subtract 1 from it.
Example:
set Points to 0
if <touching Strawberry> then
change Points by 1
Click the green flag to start, it will reset Points back to 0.
Follow the prompt...
After meeting the strawberry, use arrow keys to control the cat until it gets 20 points.
show variable [variable name]
hide variable [variable name]
Control whether a variable is visible to the users on the stage.
You can also check the box next to the variable name to control whether it is visible all the time.
Example:
show variable [Points]
hide variable [Points]
Click the green flag to start, variable Points will be hidden.
Follow the prompt...
After meeting the strawberry, the variable Points will show on the stage.
add (thing) to [list]
insert (thing) at (index) of [list]
replace item (index) of [list] with (thing)
There are 3 ways to add a new item to a list: add(append), insert, and replace.
Add will add the new thing to the end of the list.
Insert will need a location (index number) to insert the new thing. Insert will push all previous items that will be after the new thing 1 index forward (+1). Avoid using insert if you don't want the index of items constantly changing.
Replace will need the index of the old item to swap with the new thing. The old item will disappear after calling replace.
Example:
show list [Shopping list]
hide list [colors]
Click the green flag to start.
If you pick a color that is not in the list, your choice will be inserted to the top of colors list.
For shopping list, all three answers will be added to the Shopping list.
Then you will replace the first one of Shopping list with health food.
delete (index) of [list]
delete all of [list]
There are 2 ways to delete items from a list.
Delete (index) of [list] will delete the item at the given index spot of the list. The items after the deleted item will automatically move 1 index backward (-1).
Delete all of [list] will delete all items in the list and leave an empty list. It is commonly used to reset the list at the beginning.
Example:
delete (answer) of [Shopping list]
delete all of [colors]
Click the green flag to start. It will reset all lists to be empty.
Follow the prompt...
After you select 3 things, the cat will ask you to delete one. Input a number (should be in the range of 1 to 3), and the item in the given index will be deleted from the Shopping list.
Import or Export a list
Right click the list panel you will see a dropdown menu for import or export list. Sometimes, it is too tedious to add all items in the list one by one using the add (thing) to list function or the "+" sign on the list panel. You can use a .txt file to import a list easily by typing 1 item in one line.
Make sure you delete the useless empty lines, or they will also become items in the list. You can also click on the items to change the content as a text box.
To export and save your list as .txt. file, just click the export. It will download the file automatically.
show list [list name]
hide list [list name]
Control whether a list is visible to the users on the stage.
You can also check the box next to the list name to control whether it is visible all the time.
Example:
show list [Shopping list]
hide list [colors]
Click the green flag to start, the list colors will be shown but the list Shopping list will be hidden.
Once you picked your favorite color, the list colors will be hidden and Shopping list will show on the stage.
item (index) of [list]
item # of (thing) in [list]
length of [list]
[list] contains (thing)?
These 3 value blocks and a boolean block will give you the information about the list and items.
You can input an index in item() of [list] to find what item is in that location. You can also input an item in item# of () in [list] to find where the item is in the list, returning an index. The length of [list] will tell you how many items this list has.
You can also input a thing you try to check in <[list] contains ()?> to know whether it is in the list currently.
Example:
if <[colors] contains (answer)?> then
switch costume to (item# of (answer) in [colors]) + 1
Click the green flag to start.
Once you pick your favorite color, the costume number will be decided by the index of your answer in the list plus one. The first costume of the cat is the original cat, and the rest of the costumes are labeled in the same order as their color are shown in the list.