3 – Making Variables and lists in Scratch

Learning Objectives

  • Explore variables
  • Use and create programs with different variables.
  • Learn to use a list.

Learning Outcomes

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)

Keywords

Words to learn: Variable,  list, concatenate, string, integer.

Starter

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.

Main
Now open Scratch 1.4 (Instructions in the last lesson).

 

snap2 Click on the “variables” tab (top left), and click  “Make a variable”. Now type “number1” in the  dialogue box.

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.

 

You have now made two empty variables.  Now it’s time to do something with them!  Try to copy this code and click scs3 :

snap2a  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:

snap2b

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.

Try this:
snap2c Run the code and you’ll see it asks the user  for a number, and always adds 6 to it.

 

 

Exercises

  1. Your first task is to change the above code so it asks the user for TWO numbers, and adds them together.
  2. Now amend the code even more so that it asks the user for THREE numbers and adds them together.
  3. 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
  4. Now make a new program.  It must:
    1. Ask for the user’s name.
    2. Store the name in a variable called “name”
    3. Ask for the user’s age.
    4. Store the age in a variable called “age”
    5. 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.

Lists

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.

We make a list in the same way as a variable in Snap!  First make a variable called “nameList”, then save a list in it.  See if you can copy this code:
snap2d

 

 

Now run the code.
What if we want to add more values?  Easy.  Try this:

snap2e When you run this code, you should that a list is created with the first two names,  then a third is added afterwards.

 

 

 

This is just the beginning.  We can do so much more with lists.  Hopefully these exercises will let you experiment some more:

Exercises

  1. Change the program to save the name of everyone sitting in your row.
  2. 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]”
  3. 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
  4. Can you make the program choose one person from your row at random?
    **HINT**  The green menu lets you pick a random number.