Matching Things Up
Now that you’ve got the water amounts in a list, you’re going to put the sprite names in another list. Since the sprite names and the water amounts are related to each other, you’ll be using the same counter variable to keep track of which list item you’re on in both lists.
Make another list and call it Sprites
.
Add two items to the list: Tap
and Toilet
. Make sure that what you type is exactly the same as the names of the Tap
and Toilet
sprites!
Once you’re done adding things to the list, hide it from the stage.
Now look at your script with the loops and find the two go to
blocks that make the talking sprite move to the Tap
and the Toilet
sprites.
Just like you did before, drag an item
block into each one — it doesn’t look like you can place something else into the box, but you can! Give it a try.
Your code should look like this now:
Do you see that you are now using the same index to select the sprite name from a list and the matching water amount from the other list? This is the perfect opportunity to use a variable. Let’s do that now!
Create a new variable called counter
.
At the start of the program, set the value of counter
to 0
:
Drag the counter
block into the four blocks where you get an item from a list, in place of the numbers 1
and 2
.
One thing is still missing: the value of counter
is 0
at the moment. You need to set it to the correct value before you use it each time. To do this, you will add 1
. This is also known as incrementing.
Add in two change counter by 1
blocks into your code so that it looks like this:
Run your code to check that everything is still working as it should!
Here is what the full program should look like by now:
In the next step you will learn how to make your code even shorter with another clever loop!