Water Use Calculator

Repetition is Key

Right now you have two sets of code blocks in your script that are identical:

changecounterby1gotoitemcounterofSpritesmove45stepsrepeatitemcounterofWaterAmountsmove20stepsstampwait0.5seconds

If you were to add more sprites to illustrate amounts of water for other things like showers or washing dishes, this would very quickly get out of hand! It seems like these blocks should be inside a loop of some sort, right?

On your script, detach the first change counter by 1 block and everything after it, and drag it to one side (don’t delete it just yet!), so that the last blocks in your green flag script are:

switchcostumetoglass water-asetsizeto35%

Look in the Control section and take out this block:

repeatuntil
  • Attach it after the set size to 35% block.

  • Then take one group of the other blocks and put it inside the loop. You can now delete the other identical set of those blocks, as you no longer need them!

switchcostumetoglass water-asetsizeto35%repeatuntilchangecounterby1gotoitemcounterofSpritesmove45stepsrepeatitemcounterofWaterAmountsmove20stepsstampwait0.5seconds

Finally, you need to put something in the until! You want the code to repeat for each item in the lists, so a good thing to check for would be whether the counter has reached the last item in one of them. You can use another handy block here, from the list blocks in Variables:

lengthofSprites

First, take an operator block to check if two things are equal:

=

Place this into the space in the repeat until block:

repeatuntil=

Grab the counter block and plug it into the left-hand side of the operator block.

Then, from Variables, place the length of block into the right-hand side of the operator.

repeatuntilcounter=lengthofSprites

About the new loop

You’ve used the repeat with a number to tell Scratch how many times to repeat something.

But if you add more things to your lists of sprites and water amounts, you’ll have to change the code each time to update the the repeat number.

With a repeat until block, the code checks how many times it needs to repeat so that you don’t have to!

Click the green flag to see your new and improved script run — it does the same thing using half the amount of blocks!

Let’s really make the most of this new loop and make the program display some more water usage information.

Create five more sprites, naming them Shower, Bath, Dishwasher, Dishes, and Washing. You can give them more descriptive text on their costumes if you want to.

Here are the descriptions I’ve used:

Sprite name Text description
Shower 5-minute shower
Bath A bath
Dishwasher Dishwasher
Dishes Handwash dishes
Washing Washing machine

Tick the checkboxes to show both your lists on the stage so you can add the new information to them.

Add all the new sprite names to the Sprites list, making sure to type them exactly correct.

Then add the water usage to the WaterAmounts list. The table below shows the litres of water for each thing. Make sure you add them in the same order as the sprites, so that their list index numbers match up!

Sprite WaterAmount
Shower 35
Bath 80
Dishwasher 15
Dishes 10
Washing 50

The two lists full of water data

Finally, hide the lists again by unticking the checkboxes next to the code blocks, and click the green flag to run the code. You should see all the new water amounts get animated — without having to add any new code! Cool, right?

You might notice some of the items run out of space on the screen because the amount of water is so huge, and maybe you want to speed up the animation a little. The next card will show you some small tweaks you can make to fix all this.