IN THE LAST adventure, you learned about variables and lists in Scratch. Variables and lists are boxes that you use for holding numbers or text. After you have numbers or text in a program, you can do all sorts of things with them, such as adding the numbers together or joining text.
In this adventure, you learn about the Scratch blocks that make it possible to combine, change, add, multiply, divide, and transform variables and values. Welcome to the fascinating world of the operator blocks!
The Operators Block Palette contains 17 blocks. These blocks all have one thing in common: They take one or more values and use them to produce some other value. When you use values to produce other values in programming, it’s called “performing an operation.” The blocks that make operations possible are called “operators.”
Figure 7-1 shows all the operator blocks.
Operators are powerful blocks. Understanding and mastering them is the key to being able to do all sorts of useful and essential things in your projects. Some of the things they make possible include
You’ve already used some of Scratch’s operator blocks in previous adventures. Read on to learn about each of them and how to use them.
Math is a huge part of programming. You don’t need to be a math genius to write programs, but knowing how to use the math operator blocks certainly makes you look like one! This section shows you how to use each one and gives you some tips, tricks, and examples that you can start using in your own creations today.
The addition
block, shown in Figure 7-2, takes two numbers (or variables containing numbers) and adds them together.
To try out the addition
block, as well as all the other math operators, you’ll be enlisting the help of a puppy. Follow these steps:
when green flag clicked
block from the Events Block Palette to the Scripts Area.forever
block from the Control Block Palette and snap it to the when green flag clicked
block.Create two new variables in the Data Block Palette. The first one should be called myNumber
and the second one should be called myNumber2
.
Your Data Block Palette should now look like Figure 7-3.
Right-click myNumber
in the Stage.
Several options for how to display the variable appear, as shown in Figure 7-4.
Select slider from the variable display menu.
The variable display on the Stage turns into a slider, as shown in Figure 7-5.
myNumber2
on the Stage and change it to a slider also.think ()
block from the Looks Block Palette to the Scripts Area and snap it inside the forever
block.() + ()
block from the Operators Block Palette and place it into the think ()
block.myNumber
variable from the Data Block Palette and place it in the first spot in the () + ()
block. Drag the myNumber2
variable from the Data Block Palette and place it in the second spot in the () + ()
block.
Your script should now look like Figure 7-6.
Click the Green Flag and then click and drag the sliders on your variables on the Stage. The puppy instantly does the math, as you can see in Figure 7-7.
Subtraction is just addition in reverse. The () - ()
block in Scratch is shown in Figure 7-8.
To create a Subtraction Puppy project, follow these steps:
Select File ⇒ Save as a Copy from the top toolbar.
An exact copy of the project is created, with the title changed to Addition Puppy copy.
(myNumber) + (myNumber)
block out of the think ()
block in the Scripts Area, as shown in Figure 7-9.() - ()
block from the Operators Block Palette and drop it into the think ()
block.myNumber
block from the () + ()
block in the Scripts Area and place it into the first space in the () - ()
block.myNumber2
block from the () + ()
block in the Scripts Area and place it into the second space in the () - ()
block. Right-click the empty () + ()
block and select Delete.
Your script should now look like Figure 7-10.
Test the Subtraction Puppy by clicking the Green Flag and then dragging the variable sliders.
In Scratch, as well as in most other programming languages, the multiplication symbol is the *
character. Figure 7-11 shows the Scratch () * ()
block.
Here’s how to create the Multiplication Puppy program.
Replace the () - ()
block in the Scripts Area with the () * ()
block.
Your finished script should look like Figure 7-12.
If you’re not completely impressed by how fast your puppy can do addition, subtraction, and multiplication, this next display of canine intellect is going to totally win you over.
The Scratch division operator is shown in Figure 7-13.
To try out the division operator, create the Division Puppy program by following these steps:
Replace the () * ()
block in the Scripts Area with a () / ()
block.
When finished, your script should look like Figure 7-14.
Click the Green Flag and then set both of the number variables to 0 by dragging the slider handles all the way to the left.
The puppy starts thinking the characters NaN
. This stands for Not a Number. In math, we say that the result of dividing 0 by 0 is “no defined value” because there’s no way to find an answer for it. Scratch simply says that it’s not a number, which is also true.
Set the value of the first variable to a number greater than 0, but leave the second number set to 0.
The puppy starts to think the word Infinity
, as shown in Figure 7-15.
Drag the slider for myNumber
all the way to the left to set it to 0 and drag the slider for myNumber2
to any number other than 0.
The result is 0. Can you figure out why?
The Boolean operator blocks each use a value, or multiple values, to decide whether something is true or false. Boolean operating is also known as Boolean logic. You saw the Boolean blocks in Adventure 3, when you learned about their good friends, the condition blocks. To quickly review, the six Boolean blocks in the Operators Block Palette are
In Adventure 3, you learned that Boolean blocks are used inside control blocks in order to choose between multiple paths or to decide whether to keep on looping.
In this adventure, you use Boolean operator blocks by themselves to experiment with creating true and false values.
Follow these steps and watch how different operations produce true or false values.
() > ()
block from the Operators Block Palette to the Scripts Area.() > ()
block and 2 into the right side. Double-click the (3) > (2)
block in the Scripts Area.
A talk bubble appears next to the block and tells you that the result is true, that 3 is greater than 2, as shown in Figure 7-16.
Right-click the () > ()
in the Scripts Area.
In addition to the normal right-click options, you also see three more: <, =, and >, as shown in Figure 7-17.
Select the less than (<) symbol from the right-click menu.
Like magic, the () > ()
block changes into a () < ()
block, and leaves your values as they were!
Double-click the () < ()
block.
A talk box appears displaying false
, showing that 3 is not less than 2.
Using () < ()
, () = ()
, and () > ()
blocks by themselves is pretty simple, but you can also combine them to do much more complicated logic. Follow these steps to see the sorts of things that Scratch can do with logic!
() and ()
block from the Operators Block Palette to the Scripts Area.(3) < (2)
block into the first space within the () and ()
block.() > ()
block from the Operators Block Palette to the Scripts Area and snap it into the right side of the () and ()
block.() > ()
block to 99 and 1. Double-click the () and ()
block.
The result is false
, as shown in Figure 7-18, because only one of the two halves of the () and ()
block is true.
() or ()
block from the Operators Block Palette to the Scripts Area.(3) < (2)
block from the left side of the () and ()
block to the left side of the () or ()
block.(99) > (1)
block from the right side of the () and ()
block to the right side of the () or ()
block. Double-click the () or ()
block.
This block produces a true value because one of the sides of the block is true, as shown in Figure 7-19.
The text operators in Scratch can work with user-entered text, variables containing text, and just plain text in order to figure out how many characters are in it, find letters in text, and join pieces of text together.
The join () ()
block lets you combine words, and even sentences, to create custom values to store in variables or to be used by sprites.
A very common use for the join () ()
block is for mixing the values of variables into things that will be said by a sprite. To use the join () ()
block to make Scratch the Cat count potatoes, follow these steps.
potatoes
.when green flag clicked
block from the Events Block Palette to the Scripts Area.set () to ()
block from the Data Block Palette and snap it to the when green flag clicked
block.set () to ()
block is set to potatoes, and that the second space in the block contains a 0.repeat ()
block from the Control Block Palette and snap it to the bottom of the set
(potatoes) to (0)
block.repeat ()
block to 4. Drag a change () by ()
block from the Data Block Palette and snap it inside the repeat ()
block.
The drop-down menu should be set to potatoes and the second space should be set to 1.
say () for () secs
block from the Looks Block Palette and snap it to the bottom of the change (potatoes) by (1)
block inside the repeat (4)
block.join () ()
block from the Operators Block Palette into the first space in the say () for () secs
block.potatoes
variable from the Data Block Palette and place it inside the first space in the join () ()
block.join () ()
block.Click the Green Flag to start the counting.
The result on the Stage should look like Figure 7-20.
You’re almost there, but it’s strange how Scratch the Cat doesn’t have a space between the number and the word potato. It’s important to remember that Scratch doesn’t put in spaces for you when you join words together.
To fix the problem, click your mouse just before the word potato in the second space in the join () ()
block and press the space bar.
Now click the Green Flag again, and it looks much better, as shown in Figure 7-21.
The letter () of ()
block tells you what letter comes at a certain position in a word or text. To use it, follow these steps:
letter () of ()
block from the Operators Block Palette to the Scripts Area.Double-click the block.
You see a talk bubble with the first letter of your name in it, as shown in Figure 7-22.
When you combine the letter () of ()
block with the length of ()
block, you can do some pretty cool tricks. Try it out.
The length of ()
block tells you how many characters (including spaces and punctuation) there are in a piece of text.
If you want to find out what the last letter of any person’s name is, you can combine the length of ()
block with the letter () of ()
block. Try it out:
letter () of ()
block to the Scripts Area.length of ()
block to the Scripts Area and place it inside the first space in the letter () of ()
block.length of ()
block and the letter () of ()
block, as shown in Figure 7-23.Scratch can go far beyond basic math and logic. The last three blocks in the Operators Block Palette perform math functions that aren’t as common as basic arithmetic but are quite common in Scratch programs.
The () mod ()
block divides the first number by the second number and then tells you what’s left over (the remainder). For example, if you make a (7) mod (3)
block, the result will be 1, because 7 can be divided by 3 twice, with 1 left over.
Figure 7-24 shows the () mod ()
block in action.
The round ()
block rounds a number to its closest whole number. For example, the block in Figure 7-25 produces a value of 8 because 7.51 is closer to 8 than it is to 7.
This last mathematical operator block is actually 14 blocks in one! The drop-down menu inside this block has 14 possible values, as shown in Figure 7-26.
In this adventure, you create a math practice game that quizzes you with different math problems and keeps score of how well you did answering ten questions!
The first job in writing this program is to make a sprite ask what type of math you want to practice. You start with addition and multiplication, but you can expand the program later on to include any type of math you want!
when green flag clicked
block from the Events Block Palette to the Scripts Area.
Score
.set () to ()
block from the Data Block Palette and snap it to the bottom of the when green flag clicked
block.set () to ()
is Score and the second value is 0.say () for () secs
block from the Looks Block Palette to the Scripts Area and snap it to the when green flag clicked
block.say () for () secs
block to the Scripts Area and snap it to the bottom of the previous one.ask () and wait
block from the Sensing Block Palette and snap it to the bottom of the script.say () for () secs
block to Hello!say () for () secs
block to Let’s do some math!ask () and wait
block to Do you want to practice addition or multiplication (enter + or x)?The next thing you do is to create three different paths the program can take depending on the user’s answer to the first question.
To program these three branches, follow these steps:
if () then, else
block from the Control Block Palette to the Scripts Area and attach it to the ask () and wait
block.() = ()
block from the Operators Block Palette and place it in the hexagram shape in the if () then, else
block.answer
block from the Sensing Block Palette and place it into the left side of the () = ()
block.() = ()
block.if () then, else
block into the else part of the first if () then, else
block, as shown in Figure 7-28.() = ()
block into the new if () then, else
block.answer
block into the left side of the () = ()
block.() = ()
block.say () for () secs
block from the Looks Block Palette and drop it into the else
section of the nested if () then, else
block.say () for () secs
block to I didn’t understand your answer.Now you’ve created the three main branches of the program! Move on to the next section to build the addition quiz.
Follow these steps to create a scored Addition Quiz.
repeat ()
block from the Control Block Palette and drop it into the empty space in the first if () then, else
block.
Number1
and
Number2
.set () to ()
block from the Data Block Palette and snap it inside the repeat ()
block.set () to ()
block to
Number1
.pick random () to ()
block from the Operators Block Palette and snap it into the second space in the set () to ()
block.pick random () to ()
block to 100.set () to ()
block you just created and snap it to the bottom of the first one.set () to ()
block to
Number2
.ask () and wait
block to the Scripts Area and snap it to the bottom of the set (Number2) to (pick random (0) to (100))
block.join()()
block into the space in the ask () and wait
block.join () ()
block into the second space in the join () ()
block.join () ()
block into the second space of the last join () ()
block.ask () and wait
block look like Figure 7-30.if () then, else
block to the Scripts Area and snap it to the bottom of the ask () and wait
block.()=()
block into the space in the if () then, else
block.answer
block into the first part of the () = ()
block.() + ()
block into the second part of the () = ()
block.Number1
variable into the first space in the () + ()
and snap the Number2
variable into the second space in the () + ()
block.say () for () secs
block inside the space after then
, in this if () then, else
block.say () for () secs
block to Correct!change () by ()
block from the Data Block Palette and snap it to the bottom of the say () for () secs
block.change () by ()
block to Score and change the second value to 1.say () for () secs
block to the Scripts Area and snap it into the else
part of the if () then, else
block.say () for () secs
block to No, that’s not correct.The finished addition quiz, including the answer to the challenge question, is shown in Figure 7-31.
The multiplication quiz is very similar to the addition quiz. The only difference is in the messages that are displayed, and in the values. Set the range for the multiplication quiz from 0 to 12, so that it’s easier for people taking the quiz to do the math in their heads.
Can you create this part of the program yourself? Here are the basic steps:
repeat ()
block out of the script for a moment, and then duplicate it and place the duplicate into the space after if (answer = x) then
.repeat ()
block back where it was.repeat ()
block, as well as the say () for ()
block that follows it and gives the score, to match Figure 7-32.When you’re done, the whole program should look like Figure 7-33.
Learn to use operators to create more realistic games by adding gravity and acceleration to a jumping sprite. Visit www.youtube.com/watch?v=4YoGkwV7D9c
to watch a video.
Achievement Unlocked: Performing Operations
3.129.70.157