Variables and operators

Quiz time with Scratch - Lesson 1

Objectives

  • Use sequence, selection, and repetition in programs
  • Understand, explain and use variables effectively
  • Generate appropriate inputs and predicted outputs to test programs

Lesson Resources

Introduction

Scratch can be used online at scratch.mit.edu. An offline version can also be downloaded for free HERE if you would like it installed on your network. 

Everything included is also possible with previous versions of Scratch although you may find there are some layout and feature differences with older versions. 

You can set up a Scratch Teacher Account which provides teachers and other educators with additional features to manage student participation on Scratch, including the ability to create student accounts, organise student projects into studios, and monitor student comments.

Scratch is a project of the Scratch Foundation, in collaboration with the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu

 

Versions of Scratch

Scratch 1.4 - Released July 2009

Offline only. The version most schools began using as they adopted teaching and learning with Scratch. Other previous versions were available. Newer Scratch projects are not backwards compatible with this version, but files from v1.4 will open with newer versions of Scratch.

 

Scratch 2.0 - Released May 2013

Online editor was released for the first time. Offline editor was also made available later. Lots of new features made available. Online community and sharing of projects became much easier. Older Scratch projects can be opened in Scratch 2.0

 

Scratch 3.0 - Released Jan 2019

New Online editor that has been completely rewritten in HTML5 to make it compatible with all modern web browsers on most devicesScratch 2.0 projects will be compatible with Scratch 3.0. Offline editors available for some platforms on launch. iOS and Android versions to follow.

 

What is a variable?

In programming, a variable is a placeholder for a value, a box that you can store information in, much like x and y are popular variables in algebra. In Scratch, variables are represented with blocks shaped like elongated circles, that are named by you. 

Variables are values that can change, for example, in a video game you may have variables for the score, lives or time; all numeric values that may go up or down as the game runs. 

In this Pac-Man game example Points and Lives are the variables and can be displayed on the game screen. Orange blocks that reference the variables are also in the code for the Pac-Man sprite. They set the value for the ‘Lives’ variable to 3 when the game starts, and further down the code, both variables are measured with = operator blocks (we’ll look at these more later). When the ‘Lives’ variable is equal to 0 the game stops with a ‘Game over!’ message and sound effect. When the ‘Points’ variable is equal to 32 (the number of Pac Points in the maze) the player gets a ‘Congratulations!’ message.

 

 

In Scratch you find these blocks in the ‘Variables’ category, which, to begin with, contains ‘my variable’. You can make new variables by clicking ‘Make a Variable’ and then choosing the variable name from the drop down box in the blocks. 

 

 

The options for what you can do with them are fairly simple; 

  • Set the variable to a certain value, 
  • Change it (up with a positive number, down with a negative number)
  • Hide it from the screen
  • Show it on the screen

 

Operators

What do you think these block do? Are there any symbols on them you recognise? Discuss these with the students and see how many they can work out for themselves. Operator blocks are colour-coded light-green, and are used to script maths equations and string handling. You can obviously type into the white box spaces on them, but you can also add certain other Scratch blocks to the space, such as variable name blocks (which was shown in the Pac-Man example above). The most common blocks we will use in this unit are listed below:

 

Adds two numbers

 

Subtracts second number from first number

 

Multiplies two numbers

 

Divides second number by the first number

 

Reports ‘True’ if first value is less than the second value

 

Reports ‘True’ if first value is equal to the second value

 

Reports ‘True’ if first value is more than the second value

 

Picks a random number in a range specified by you.

 

This block joins two strings together and reports the result — for example, if "hello" and "world" were put in the block, it would report "helloworld"

 

Questions and answers (Inputs / Outputs)

The ‘Ask ...... and Wait’ block is a Sensing block and a Stack block

 

 

The block will make an input box (with the specified text above it) show at the bottom of the stage screen. Users can then input text into it and submit it. The input is then stored in the Answer block. The Answer block automatically updates to most recent input and can only hold one answer at a time.

 

 

Here’s a simple example:

When the script is started we are asked our name and the input box appears at the bottom of the stage screen.

 

The answer is submitted by typing in the box and pressing the enter key or clicking the blue tick on the input box:

 

 

The sprite reports back the string ‘Hi’ and our answer as we used a join block to put these two strings together.

 

A simple maths quiz

The idea behind this whole unit came from a Scratch times tables project made by Miles Berry, the Principal lecturer in Computing at Roehampton University. You can find a walk through video of Miles explaining how to create the project HERE, plus some nice ideas to extend the game. This might be useful to watch to get started with things.

 

 

Explain to the students that over the next few weeks we are going to use these blocks (and others) to create a number of quizzes with Scratch. Let’s look at how we can combine some of these concepts to make a simple times tables test project (you can try it out here). Today we’ll build it together and then at the end you can think of ways to add to and improve it.

 

 

We don’t want it to ask the same question every time, so we are going to create two variables; ‘a’ and ‘b’. These will represent the two numbers that we will multiply together. Using a variable for them will mean that we can have different numbers each time. 

Ask the students to go to the variables category, make a new variable and call it ‘a’. Repeat this and call the second variable ‘b’.

 

   

 

Add a ‘when the green flag is clicked’ block and two ‘Set variable’ blocks. Choose variable ‘a’ for one and ‘b’ for the other. Next add a ‘pick random number’ block into the number box: 

 

 

A and B will be multiplied together, so these random number blocks will dictate the range of numbers that can be used in our multiplication questions, what do you think the number range should be? A range of 1 to 12 means that it will cover questions from 1 x 1 up to 12 x 12, which should be sufficient for KS2. 

Ask them to try this out; every time they click the flag they should get a different pair of numbers between 1 and 12 showing in the variable boxes on the stage. 

Next we want to ask the question, so we’re going to need an ‘ask and wait’ block. It won’t work if we just type in the box ‘What is a x b?’ as Scratch won’t relate the ‘a’ and ‘b’ you type to the variables we’ve just created, so we need to use some join blocks to connect a few strings together. You will actually need four join blocks and a variable name block for ‘a’ and ‘b’. Can you put these blocks together correctly so it asks the question ‘What is a x b?’

 

 

And it all goes inside the ‘ask’ block to give you this:

 

 

Add spaces before and after your typed strings to make sure they appear separated when the question appears on the screen. Add this to the script we already had and try it out. Every time you click the flag you should get a new multiplication question:

 

 

So we have the question part of our project, what should come next? What is not working at the moment? If the students have tried to add an answer to the input box they will have noticed that nothing happens yet. So we need to code it to check our answer and tell us if we are correct or not. 

Ask the students to work with the person next them for a few minutes to see if they can come up with any ideas for how to code this.

There are two possible outcomes when a user adds an answer; they get it right or wrong! So we need to allow for both these outcomes with our code and use an ‘if/else’ block:

 

 

If the condition is true, the script will runs the blocks inside the ‘if’ portion; if not, runs the blocks inside the else portion.

We’re going to use it to say ‘if the answer is correct, say “well done”, else (when the answer is wrong), say “sorry that’s not right”.

Again, see if they can figure out where it all goes, here’s the block they need:

 

 

Here’s how it should look, along with the other code we’ve already written. And to finish it off, we’ll add a repeat block around everything so we get ten questions in our tables quiz:

 

 

You can see and try out a working version of this quiz HERE.

At this point the students can consider what they would add to this project to improve it. 

They may want to improve the visual side of it’s design by changing the sprite and perhaps adding costumes or animation to it. They might also add an appropriate background to the stage and other visual or sound effects for when answers are right or wrong, or try to incorporate the idea of a race as featured in Miles’ video.

Functionally, one clear area for progression is to for it to give you a score out of ten at the end. A new variable for ‘Score’ needs to be created for this and a ‘change variable by 1’ added as they get answers correct. Remember a ‘Set score to 0’ block at the top to reset the score each time someone plays and then a final line of code to report back the score variable after ten questions then finishes things off:

 

 

Here’s a finished version of the quiz with a score counter and another using Mile Berry’s idea of a race.

Recap things at the end of the lesson and explore the students’ understanding of what we’ve done. How could you make your quiz easier for users? Smaller number range. How could you make it harder? Larger number range, time limit. If you wanted to change it to an addition quiz what would you change in your code? Operators and, depending on the targeted age group of the users, the number range to two or three digit numbers.

What is a variable? What variables did we create today and what were they used for in our code?

 

Next Lesson >