# Introduction

Todays project is a Calculator that will be able to do simple addition, subtraction, multiplication and division.

There will be a Sprite for each number button and for each type of sum, plus an equal and a clear button, so lots of Sprites. The finished Calculator might look something like this: ## The Plan

As always, it’s a good idea to have a think about what Sprites, Variables and Code you might need, having a think about the Calculator, we came up with the following list:

• Sprites
• 1 Sprite for each number button and one for the decimal point, so 11 in total.
• 1 Sprite for each of the sum buttons, +, -, X and / so 4 in total.
• 1 Sprite for the equals button.
• 1 Sprite for the clear button.
• Variables
• 2 Variables, one for each number in the sum.
• 1 Variable for the sum type.
• 1 Variable for the full sum being calculated.
• 1 Variable for the answer.
• 1 Variable to indicate which number variable we should use.
• Code
• Code for a number buttons.
• Code for the sum type buttons.
• Code for the equal button.
• Code for clear button.

We also thought that it would be a good idea to complete only 1 number button and 1 sum type button and make sure that was all working before adding the rest of the buttons.

## The Variables

We created all the variables we would need first: • Variables
• NumberOne – this is for the first number in the sum
• NumberTwo – this is for the second number in the sum
• Flag – this is to indicate which number we should be using when a button is pushed.
• Operator – this is to indicate what sum, + – X or / we should be doing.
• Question – this is to hold the complete sum, e.g. 10+23

## The First 3 Sprites

To get things started we created a number Sprite, a plus (+) Sprite and an equal Sprite

All the Sprites are very simple, just a square with text in the middle, just remember to make sure that there is a background colour as well, so the When Clicked code will work correctly.

The code for the Number Sprite looks like this: The code will check the Flag variable and then add 1 to either the NumberOne or NumberTwo variable, it will also add 1 to the Question variable.

Note that the number 1 is added to whatever is already in the number or question variables.

The code for the + Sprite is very simple: We just set the Flag to indicate that we should move on to NumberTwo, we set the Operator to + and finally we put the + in the Question as well.

The code for the equal Sprite again is quite simple as we only have one sum type. We check the value of the Operator and if it is + we add the NumberOne and NumberTwo variables together and put the result in the Answer Variable.

## The other Numbers and Sum types

Once we had the number one, the + and the equal Sprites working it was quite simple to copy and create the other number Sprites. The process was:

• Right click on the Number One Sprite and Copy it.
• Change the Costume of the Sprite to the next Number.
• Change the code of the new Sprite to the next Number.

The bits of the code that we changed are circled in red here: The other sum types we did the same way, copy the + Sprite and adjust the code circled in red: As we added the different sum types we also had to add more code to the Equal Sprite, which ended up looking like this, where there is one IF for each sum type: ## The Clear Button

In order to do more sums we also added a clear button, again it is a simple Sprite with some text on it, the code looked like this: This code was placed on the Stage, as we needed to run it when we clicked on the Green Flag as well as when we clicked on the Clear Sprite.

Notice that most of the variables start with nothing in them, so they are blank, it is only the Flag that needs to have 0 in it.

## Notes

The project that we built is available on the Scratch Web Site: