image

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!

Saying Hello to the Operators

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.

image

Figure 7-1 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

  • Keeping score
  • Joining words together to customize the things sprites say
  • Making sprites move in a realistic way
  • Solving math problems
  • Checking answers in quiz programs
  • Balancing the books in financial projects
  • Randomizing the positions and movements of obstacles and monsters in games

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.

Doing the Math

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.

Addition

The addition block, shown in Figure 7-2, takes two numbers (or variables containing numbers) and adds them together.

image

Figure 7-2 The addition operator block

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:

  1. Click File  ⇒  New from the top toolbar to create a new project.
  2. Click the Choose Sprite from Library icon in the New Sprite menu to open the Sprite Library.
  3. Locate the sprite named Dog Puppy and add it to your project.
  4. Right-click Scratch the Cat and select Delete to remove her from your project.
  5. Drag a when green flag clicked block from the Events Block Palette to the Scripts Area.
  6. Drag a forever block from the Control Block Palette and snap it to the when green flag clicked block.
  7. 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.

  8. Right-click myNumber in the Stage.

    Several options for how to display the variable appear, as shown in Figure 7-4.

  9. Select slider from the variable display menu.

    The variable display on the Stage turns into a slider, as shown in Figure 7-5.

  10. Right-click myNumber2 on the Stage and change it to a slider also.
  11. Drag a think () block from the Looks Block Palette to the Scripts Area and snap it inside the forever block.
  12. Drag a () + () block from the Operators Block Palette and place it into the think () block.
  13. Drag the myNumber variable from the Data Block Palette and place it in the first spot in the () + () block.
  14. 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.

  15. Change the title of your project in the space above the Stage to Addition Puppy and select File  ⇒  Save from the top toolbar.
image

Figure 7-3 Creating two variables

image

Figure 7-4 Variable display options

image

Figure 7-5 A variable slider display

image

Figure 7-6 The finished script

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.

image

Figure 7-7 The Smart Puppy doing addition

Subtraction

Subtraction is just addition in reverse. The () - () block in Scratch is shown in Figure 7-8.

image

Figure 7-8 The subtraction operator block

To create a Subtraction Puppy project, follow these steps:

  1. 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.

  2. Change the title of the project to Subtraction Puppy.
  3. Drag the (myNumber) + (myNumber) block out of the think () block in the Scripts Area, as shown in Figure 7-9.
  4. Drag a () - () block from the Operators Block Palette and drop it into the think () block.
  5. Drag the myNumber block from the () + () block in the Scripts Area and place it into the first space in the () - () block.
  6. Drag the myNumber2 block from the () + () block in the Scripts Area and place it into the second space in the () - () block.
  7. Right-click the empty () + () block and select Delete.

    Your script should now look like Figure 7-10.

image

Figure 7-9 Dragging the () + () block out of the think () block

image

Figure 7-10 The Subtraction Puppy script

Test the Subtraction Puppy by clicking the Green Flag and then dragging the variable sliders.

Multiplication

In Scratch, as well as in most other programming languages, the multiplication symbol is the * character. Figure 7-11 shows the Scratch () * () block.

image

Figure 7-11 The multiplication operator block

Here’s how to create the Multiplication Puppy program.

  1. Select File  ⇒  Save as a Copy from the top toolbar.
  2. Change the title of the project to Multiplication Puppy.
  3. Replace the () - () block in the Scripts Area with the () * () block.

    Your finished script should look like Figure 7-12.

image

Figure 7-12 The finished Multiplication Puppy program

Division

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.

image

Figure 7-13 The division operator block

To try out the division operator, create the Division Puppy program by following these steps:

  1. Create a copy of the Multiplication Puppy program by selecting File  ⇒  Save as a Copy from the top toolbar.
  2. Change the title of the program to Division Puppy and then select File  ⇒  Save.
  3. Replace the () * () block in the Scripts Area with a () / () block.

    When finished, your script should look like Figure 7-14.

  4. 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.

  5. 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.

  6. 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?

image

Figure 7-14 The Division Puppy program

image

Figure 7-15 Division Puppy, contemplating Infinity

Coding Logically

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

  • () > (). The greater than block produces a value of true if the first number is bigger than the second.
  • () < (). The less than block produces a value of true if the first number is smaller than the second.
  • () = (). The equal to block produces a value of true if the values (which may be numbers or text) on both sides of the equals sign are the same.
  • () and (). The AND block produces a value of true if the statements on both sides of the AND are true.
  • () or (). The OR block produces a value of true if either statement to the right or to the left of OR is true.
  • not (). The NOT block produces a value of true if the result of the block inside of it is false.

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.

  1. Drag a () > () block from the Operators Block Palette to the Scripts Area.
  2. Enter 3 into the left side of the () > () block and 2 into the right side.
  3. 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.

  4. 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.

  5. 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!

  6. Double-click the () < () block.

    A talk box appears displaying false, showing that 3 is not less than 2.

image

Figure 7-16 Finding out whether the number 3 is larger than the number 2

image

Figure 7-17 Additional right-click options

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!

  1. Drag a () and () block from the Operators Block Palette to the Scripts Area.
  2. Snap your (3) < (2) block into the first space within the () and () block.
  3. Drag a () > () block from the Operators Block Palette to the Scripts Area and snap it into the right side of the () and () block.
  4. Change the values in the () > () block to 99 and 1.
  5. 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.

  6. Drag a () or () block from the Operators Block Palette to the Scripts Area.
  7. Drag the (3) < (2) block from the left side of the () and () block to the left side of the () or () block.
  8. Drag the (99) > (1) block from the right side of the () and () block to the right side of the () or () block.
  9. 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.

image

Figure 7-18 Only one part is true, so the () and () block is false

image

Figure 7-19 Only one part is true, so the () or () block is true

Operating on Text

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.

Combining Text with join () ()

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.

  1. Click File  ⇒  New in the top toolbar to create a new project.
  2. In the Data Block Palette, create a new variable called potatoes .
  3. Drag a when green flag clicked block from the Events Block Palette to the Scripts Area.
  4. Drag the set () to () block from the Data Block Palette and snap it to the when green flag clicked block.
  5. Make sure that the drop-down menu in the set () to () block is set to potatoes, and that the second space in the block contains a 0.
  6. Drag a repeat () block from the Control Block Palette and snap it to the bottom of the set (potatoes) to (0) block.
  7. Change the value in the repeat () block to 4.
  8. 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.

  9. Drag a 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.
  10. Drag a join () () block from the Operators Block Palette into the first space in the say () for () secs block.
  11. Drag the potatoes variable from the Data Block Palette and place it inside the first space in the join () () block.
  12. Type the word potato into the second space in the join () () block.
  13. Click the Green Flag to start the counting.

    The result on the Stage should look like Figure 7-20.

image

Figure 7-20 Joining a variable and text

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.

image

Figure 7-21 Don’t forget to use spaces!

Finding Letters

The letter () of () block tells you what letter comes at a certain position in a word or text. To use it, follow these steps:

  1. Drag a letter () of () block from the Operators Block Palette to the Scripts Area.
  2. Enter the number 1 into the first space and your name into the second space in the block.
  3. Double-click the block.

    You see a talk bubble with the first letter of your name in it, as shown in Figure 7-22.

image

Figure 7-22 Finding out the first letter in your name!

When you combine the letter () of () block with the length of () block, you can do some pretty cool tricks. Try it out.

Getting Text Length

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:

  1. Drag a letter () of () block to the Scripts Area.
  2. Drag a length of () block to the Scripts Area and place it inside the first space in the letter () of () block.
  3. Type your name into both the length of () block and the letter () of () block, as shown in Figure 7-23.
  4. Double-click this block to find out the very last letter in whatever text you entered into both spaces.
image

Figure 7-23 A block to get the last letter of a name

Understanding Other Operations

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.

() mod ()

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.

image

Figure 7-24 Using the () mod () block

round ()

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.

image

Figure 7-25 Rounding with Scratch

() of ()

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.

image

Figure 7-26 The () of () block has many functions

Make a Math Practice Game

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!

  1. Select File  ⇒  New from the top toolbar to create a new project.
  2. Drag a when green flag clicked block from the Events Block Palette to the Scripts Area.
  3. Create a new variable in the Data Block Palette called Score .
  4. Drag a set () to () block from the Data Block Palette and snap it to the bottom of the when green flag clicked block.
  5. Make sure that the first value in the set () to () is Score and the second value is 0.
  6. Drag a say () for () secs block from the Looks Block Palette to the Scripts Area and snap it to the when green flag clicked block.
  7. Drag another say () for () secs block to the Scripts Area and snap it to the bottom of the previous one.
  8. Drag an ask () and wait block from the Sensing Block Palette and snap it to the bottom of the script.
  9. Change the value of the first say () for () secs block to Hello!
  10. Change the value of the second say () for () secs block to Let’s do some math!
  11. Change the question in the ask () and wait block to Do you want to practice addition or multiplication (enter + or x)?
  12. Your script should now look like Figure 7-27.
image

Figure 7-27 Getting things started in the math practice program

Programming Different Paths

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.

  • If the user enters +, the program takes the user to the addition quiz.
  • If the user enters x, the program takes the user to the multiplication quiz.
  • If the user enters anything other than x or +, Scratch the Cat says that she doesn’t understand.

To program these three branches, follow these steps:

  1. Drag an if () then, else block from the Control Block Palette to the Scripts Area and attach it to the ask () and wait block.
  2. Drag a () = () block from the Operators Block Palette and place it in the hexagram shape in the if () then, else block.
  3. Drag the answer block from the Sensing Block Palette and place it into the left side of the () = () block.
  4. Enter the + symbol into the right side of the () = () block.
  5. Drag an if () then, else block into the else part of the first if () then, else block, as shown in Figure 7-28.
  6. Drag a () = () block into the new if () then, else block.
  7. Drag an answer block into the left side of the () = () block.
  8. Enter a lowercase x into the right side of the () = () block.
  9. Drag a say () for () secs block from the Looks Block Palette and drop it into the else section of the nested if () then, else block.
  10. Change the value in the say () for () secs block to I didn’t understand your answer.
  11. Your script should now look like Figure 7-29.
image

Figure 7-28 Creating a nested condition

image

Figure 7-29 The conditions are all right

Now you’ve created the three main branches of the program! Move on to the next section to build the addition quiz.

Making the Addition Quiz

Follow these steps to create a scored Addition Quiz.

  1. Drag a repeat () block from the Control Block Palette and drop it into the empty space in the first if () then, else block.
  2. Create two new variables in the Data Block Palette: Number1 and Number2 .
  3. Drag a set () to () block from the Data Block Palette and snap it inside the repeat () block.
  4. Set the first value in the set () to () block to Number1 .
  5. Drag a pick random () to () block from the Operators Block Palette and snap it into the second space in the set () to () block.
  6. Change the second value in the pick random () to () block to 100.
  7. Duplicate the set () to () block you just created and snap it to the bottom of the first one.
  8. Change the first value in this new set () to () block to Number2 .
  9. Drag an 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.
  10. Drag a join()() block into the space in the ask () and wait block.
  11. Drag another join () () block into the second space in the join () () block.
  12. Drag one more join () () block into the second space of the last join () () block.
  13. Make your ask () and wait block look like Figure 7-30.
  14. Drag an if () then, else block to the Scripts Area and snap it to the bottom of the ask () and wait block.
  15. Drag a ()=() block into the space in the if () then, else block.
  16. Drag an answer block into the first part of the () = () block.
  17. Drag a () + () block into the second part of the () = () block.
  18. Snap the Number1 variable into the first space in the () + () and snap the Number2 variable into the second space in the () + () block.
  19. Drag a say () for () secs block inside the space after then, in this if () then, else block.
  20. Change the value of this say () for () secs block to Correct!
  21. Drag a change () by () block from the Data Block Palette and snap it to the bottom of the say () for () secs block.
  22. Change the first value in the change () by () block to Score and change the second value to 1.
  23. Drag a say () for () secs block to the Scripts Area and snap it into the else part of the if () then, else block.
  24. Change the value of this say () for () secs block to No, that’s not correct.
image

Figure 7-30 The finished addition question

The finished addition quiz, including the answer to the challenge question, is shown in Figure 7-31.

image

Figure 7-31 The finished addition quiz

Making the Multiplication Quiz

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:

  1. Drag the 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.
  2. Put the original repeat () block back where it was.
  3. Modify the contents of the new repeat () block, as well as the say () for () block that follows it and gives the score, to match Figure 7-32.
image

Figure 7-32 The finished multiplication quiz

When you’re done, the whole program should look like Figure 7-33.

image

Figure 7-33 The finished Math Quiz Program

Further Adventures in Coding

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.

image

Achievement Unlocked: Performing Operations

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.70.157