- Explore variables
- Use and create programs with different variables.
- Learn to use a list.
All must create a simple program asking questions and taking replies, saving the answers to memory as variables (Level 4)
Most should complete the above, and concatenate the answers in new sentences. create a list to hold names. (Level 5)
Some could complete the above, but develop the list into a program which selects a name from the list at random. (Level 6)
Words to learn: Variable, list, concatenate, string, integer.
Watch the first 1.5 minutes of this video:
This video gives you a very good idea of the basic workings of a variable. Think of the variable (really a space in computer memory with its own name) as a container. The contents may change, but the container stays the same.
Now open Scratch 1.4 (Instructions in the last lesson).
Repeat this, and create a “number2” variable.
You will see a little box with the name and value stored in your variables appears in the output window on the right.
This is great! We save a value into each of our variables, and when we run the program, it adds the two variables together. This will work whatever values we put in the “set” commands. Try changing these values and see what happens.
Now, we need to print more than just a number to the screen. We need some way of joining a message to the answer we already printed.
You won’t be surprised to see we use the “join” command (green menu). Put this code into the space in the “say” block above:
This process of joining two phrases, or “Strings” of text together has a special name. We call it “concatenating” text.
This program isn’t very useful yet – unless we change the code it always adds the same two numbers. We need it to:
- Ask the user for the two numbers.
- Add them together.
- Print out the result.
- Your first task is to change the above code so it asks the user for TWO numbers, and adds them together.
- Now amend the code even more so that it asks the user for THREE numbers and adds them together.
- Can you make the code add number1 and number2, then multiply the result by number3?Take a screenshot of your code and put it in your folder/book
- Now make a new program. It must:
- Ask for the user’s name.
- Store the name in a variable called “name”
- Ask for the user’s age.
- Store the age in a variable called “age”
- Print out a message, saying “Hello [name], you were born in [year of birth]”**HINT** If we know how old somebody is, we can subtract their age from 2015. This will give use their year of birth.Print this program out for you book/folder.
Now watch the second part of the video, from 1:28.
The video talks about arrays, but Scratch uses a very similar type of variable called a List.
Lists are like containers which hold variables.
Now run the code.
What if we want to add more values? Easy. Try this:
This is just the beginning. We can do so much more with lists. Hopefully these exercises will let you experiment some more:
- Change the program to save the name of everyone sitting in your row.
- Can you make the program output (say) the names of just the third and fourth people in your row
**HINT** There is a block of code called “item  of [list]”
- Can you concatenate the names so the output reads “Fred, Sam, Alex and Ella are sat in your row” ?
**HINT** You’ll need to use “join” inside another “join” block (this is called “nesting“)Print your program out for your book. You must make notes on your work so you can understand what the different blocks do when you come back to the code later!Extension
- Can you make the program choose one person from your row at random?
**HINT** The green menu lets you pick a random number.