Water Use Calculator

Getting Information From the User

On this card you’ll make the water usage calculator. To personalise the calculation for your program’s user, you’ll use a new block to ask them questions that they can type in answers to.

Open a new Scratch project and go to the Costumes tab for the cat sprite.

Add a new costume from the library. Choose a character that will do some talking. I’m using this monkey:

The talking monkey costume

Click on the Scripts tab and add the following blocks to your sprite. You will find the ask and wait block in the Sensing category.

whenclickedsetsizeto80%switchcostumetomonkey-aaskWhat's your name?andwait

Change the question to How many times do you flush the toilet each week?.

You’ve asked the user for information — now you need to get the answer.

First, create a variable called flushes. You will use it to store the user’s answer.

Add a variable in Scratch

  • Click on Variables in the Code tab, then click on Make a Variable.

    Variable blocks

  • Type in the name of your variable. You can choose whether you would like your variable to be available to all sprites, or to only this sprite. Press OK.

    Create variable

  • Once you have created the variable, it will be displayed on the Stage, or you can untick the variable in the Scripts tab to hide it.

    Variable on the stage

Drag out the set flushes to block from Variables.

Then, look in the Sensing section and find the answer block. This is a special variable where a Scratch program puts the most recent answer it’s received from an ask and wait block.

Plug the block into your code like this:

whenclickedsetsizeto80%switchcostumetomonkey-aaskHow many times do you flush the toilet each week?andwaitsetflushestoanswer

Time for a bit of math! First you need somewhere to store a total.

Create another variable called totalWater and set its value to 0 at the start of the program.

whenclickedsettotalWaterto0

Go to Operators and look for this block:

*

It lets you multiply two numbers.

Drag it into a change totalWater by block, like this:

changetotalWaterby*

Plug your flushes block into one side of the operator block and on the other side, type in the number 6. 6 litres is roughly how much water is used for one toilet flush.

whenclickedsettotalWaterto0setsizeto80%switchcostumetomonkey-aaskHow many times do you flush the toilet each week?andwaitsetflushestoanswerchangetotalWaterbyflushes*6

Finish off the script with a couple of say blocks to tell the user the result! You’ll find the join block in Operators.

whenclickedsettotalWaterto0setsizeto80%switchcostumetomonkey-aaskHow many times do you flush the toilet each week?andwaitsetflushestoanswerchangetotalWaterbyflushes*6sayYou use...for2secondssayjointotalWater litres of water each week!for5seconds

Click the green flag to test your code.