AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Variables in games

Week 10 · Unit 4: Variables in programming · 3 lessons
1

Choices with variables

Computing 6: Unit 4, pp. 52–67
GoalUse comparisons with variables and answers in if-then-else blocks and predict which branch runs.
New words
condition · a question in an if block that is either true or falsetrue and false · the only two answers that a condition can givecomparison · a test such as =, > or <, which compares two valuesattempts · a variable that counts how many tries someone has made
Explanation

A decision in Scratch uses an if block with a condition. The condition is a comparison, such as (answer) = [moon] or (score) > (10). A comparison is either true or false. If it is true, the blocks inside the if part run. If it is false, the blocks inside the else part run. Comparisons can use a variable, the answer block or a plain number or word. Scratch ignores the difference between capital and small letters, so MOON and moon are equal. A variable can also keep count of what the user does. Here the variable attempts goes up by 1 each time the user answers.

Script 1whenclickedsetattemptsto0Script 2when this sprite clickedaskWhat is the secret word?and waitchangeattemptsby1sayThe door opens!for2secondssayWrong word. Try again.for2secondsifanswer=moonthenelse
Worked examples
TryUser typesattemptsanswer = moon?The sprite says
1sun1falseWrong word. Try again.
2moon2trueThe door opens!
Trace the program after the flag is clicked. The user clicks the sprite and types sun, and then clicks again and types moon.
sayLevel up!for2secondsifscore>10then
A comparison can use a number. The condition (score) > (10) is true when the score is 11 or more. It is false when the score is exactly 10, because 10 is not more than 10.
Be careful with the signs. The sign = tests whether two values are equal, and > and < test which one is bigger. In a set block the word to puts a value into a variable. It does not test anything.
Class activity

Door game (no computer): an adult chooses a secret word and writes it on a card, hidden under a cup. You are the sprite. Say ‘What is the secret word?’ and let a friend guess. Keep an attempts counter with pebbles in a bowl. Add one pebble for every guess. If the guess matches the card, say ‘The door opens!’. If not, say ‘Wrong word. Try again.’ How many pebbles did it take? Then change the rule so that the door also closes for good after 3 wrong tries. If you have a computer or tablet, build the program in Scratch.

Practice
1
What does the sprite say each time? The user types Tiger, then lion.
when this sprite clickedaskWhich big cat has stripes?and waitchangetriesby1sayYes!for2secondssayNot that one.for2secondsifanswer=tigerthenelse
2
The variable score holds 10. Is the condition true or false? What about when score holds 11?
score>10
3
Write the blocks for this: if the age is less than 12, the sprite says Child ticket. Otherwise it says Adult ticket.
4
Nodira writes if <(score) = (10)> but she wants the message to appear when the score is 10 or more. What should she change?