⭐ 1.5.1 Addition
The addition block
takes two numbers (or variables containing numbers) and returns the sum.
Activity: Addition
To try out the addition block, as well as the other math operators, you will ask the help of a puppy.
The following activity adds two values stored in two variables called my Number and myNumber2 and the puppy tells the result.
Follow these steps:
- Click File ⇒ New from the top menu to
create a new project.
- Click the Choose
a Sprite icon
in the Sprite menu to open the Sprite Library.
- Locate the sprite named Puppy and click on it to add it to your project.
- From the project, right-click the Cat and select delete
to remove her from your project.
- Drag a
(when green flag clicked) block from the Events Block Palette to the Scripts Area.
- Drag a
(forever) block from the Control Block Palette and snap it to the
block.
- Create two new
variables in the
“Variables” Block Palette and then click on
. The first should be called my Number and the second one myNumber2.
Figure 22: Creating two variables
Your Variables Block Palette should now look like Figure 22.
- Right-click on
in the Stage.
- Several options for how to display the variable appear:
- Select slider from the variable display menu.
- The variable display on the Stage turns into a slider.
10. Right-click myNumber2 on the Stage and change it into a slider as well.
11.
Drag a “think ()” block from the Looks Block Palette to
the Scripts Area and snap it inside the “forever” block as shown
below:
12.
Drag a “() + ()” block from the Operators Block Palette
and place it into the
“think ()” block.
13.
Drag the “my Number” variable from the Variables
Block Palette into the first spot in the block. Drag
the “myNumber2” variable from the “Variables” Block Palette into the
second spot in the
block.
- Your script should now look like Figure 23.
Figure 23: Script for addition
14. Change the title of your project in the space above the Stage to Addition Puppy and select File ⇒ Save from the top toolbar.
Click the Green Flag and then click and drag the sliders on your variables on the Stage. The puppy instantly does the math and tells you the answer (Figure 24).
Figure 24: The puppy doing addition