STORY PLAY STORE

Learn to Code through Gameplay

3

Assign values to a variable

Video Example:

Material required: Download material .pdf

Whiteboard and marker
Whiteboard and marker
Assignment block
Assignment block
Code block
Code blocks
Variable block
Variable block
program BEGIN block
BEGIN block
program END block
END block
Two dice: one dotted and another with numbers
Die of choice
Die numbers
Die numbers
Numbers
Numbers
Print function call
Print function call
Random function call
Random function call ('die roll')

  Read       Write

Use the die Small dotted die and the 'die numbers' Small die numbers with younger children. Children typically begin to understand abstract numbers around 4 or 5 years old.
1

What is a variable?

A variable can be defined as a container of values.

a variable block with value 5

The variable 'a' has the value 5.

i variable block with value 3

The variable 'i' has the value 3.

2

How do I assign a value to a variable?

To assign a number to variable 'a' use an assignment block:

1

Place a number inside the assignment block:

Program to assign a value to a variable
2

Slide the variable into the assignment block.

Variable 'a' entering assignment block
3

"Assigning value 2 to variable 'a'".

Assigning value 2 to variable 'a'
4

Slide the variable with the value 2 out of the assignment block.

Sliding variable out of the assignment block
5

Try assigning other values to variable 'a'.

3

How do I assign a random value to variable 'a'?

Random function call
Rolling die

The 'random' function returns a number between 1 and 6. It's like rolling a die. Use it to assign a value between 1 and 6 to variable 'a' as follows:

1

Place the call to the random function inside the assignment block.

Random function call inside an assignment block
2

Slide the variable into the assignment block.

Variable entring assignment
3

"Calling the random function" (roll the die).

Random function call in assignment block
4

"Asigning value 4 to variable 'a' " (assign the die value to the variable).

Assigning random value to variable block
5

Slide the variable with the value 4 out of the assignment block. "End of the program."

End of a program
6

Run the program again to assign other random values to variable 'a'.

4

How do I print the value of a variable on my whiteboard/terminal?

1

Place the blocks as follows and run the program:

Print a variable function call in code block
2

As you've already learned, assign a value to variable 'a' using the assignment block:

- "Calling the random function" (roll the die), "Assigning value 6 to variable 'a'"

Assign roll die to variable
3

Then continue reading the program in the direction of the arrows.

Block programming direction arrows
Variable entering into code block
4

- "Calling the 'print' function"

Print (draw) the value of variable 'a' on your whiteboard:

Calling print function and printing on whiteboard
5

"End of the program".

End block after code block
6

Run the program again.