We will also program the power function in our calculator by doing appropriate numbers of multiplication - we will see how to do this later.
Right now, check out the Happy Calculator program below!
I call the yellow fellow "Happy". Happy asks three questions: first to enter a number, then he asks for an operator, followed again by a number. He will then do the math on both numbers.
For example, we might do this:
|Happy: Enter first number
We type 5 (and press Enter, or click the tick icon)
Happy: Enter an operator
We type *
Happy: Enter second number
We type 6
Happy goes on to multiply 5 by 6, and shouts out the result, which is 30.
Here are the operators Happy knows about: +, -, *, / and ^
I am sure we are already familiar with the plus (+) and and minus (-) symbols.
At school, we write our multiplications with the x symbol e.g. 3 x 5 = 15
Here we will use * as the multiplication symbol. We will use / as the division symbol, and ^ as the power symbol. We will program Happy to understand these symbols.
More examples of math with our operator symbols:
|2 to the power of 3||: 2 ^ 3 = 8|
|100 divided by 5||: 100 / 5 = 20|
|13 multiplied by 6||: 13 * 6 = 78|
Alright, now we are ready to program our calculator! I suggest downloading the project and have a look at the full script.
Step 1: We need variablesWe know that we are programming Happy to ask for input. When someone enters an input, we have to store it somewhere, or it just vanishes and our program will have no idea what the input was. To do this, we create variables. You could think of variables as boxes to keep things. We may store input in variables, or just any value we want. Here, we create five variables:
- err that stores the sentence "Oops, I can't calculate this!" This is what Happy will say when something goes wrong.
- num1 that stores the first input number.
- num2 that stores the second input number.
- op that stores the input operator.
- ans that stores the final result of our calculation.
At the start, we set these variables to their initial values:
|Setting variables to initial values|
Step 2: Happy asks for inputHaving set those variables, we program Happy to ask for user input:
|Asking for and getting input|
Notice that we use several broadcast blocks in our script. They are just there to signal the instructions sprite to appear and disappear. They are not the main focus of our calculation script, but you could always comment on this post if you need help with them.
Step 3: We do math!
|Script for calculation|
Addition, subtraction and multiplicationWhen the operator is +, - or *, we do the necessary math with Scratch's operator blocks, and set the ans variable to the result of the operation.
DivisionWhen the operator is /, we know we are doing division. However, we can only divde when divisor is not zero, so we add an extra check:
|Division with a check for valid divisor|
PowerScratch does not have an operator block for the power operation so we are going to work around this. As we know, the power operation simply means a string of multiplication on the base number e.g. 2^6 is 2 multiplied by itself 6 times or 2*2*2*2*2*2. 9^3 is 9 multiplied by itself 3 times, or 9*9*9. Anything to the power of zero is 1 e.g. 6^0 = 1, 541^0 = 1.
|Repeated multiplication to do power arithmetic|
|At the start, ans is 1
After loop 1 of repeat block ans is 1*9 = 9
After loop 2 of repeat block ans is 9*9 = 81
After loop 3 of repeat block ans is 81*9 = 729
Suppose if num2 is zero, then the repeat block repeats for zero times, meaning that no further multiplication is done at all, and so ans remains at 1.
Invalid operatorIf after comparing against all our five operators we cannot find one that matches the input, then we set ans to err.
|Error when there is an invalid input operator|
Step 4: Happy reveals the answerBy the end of the script, all that Happy does is to say the the value of ans. If it isn't an err value, then switch Happy's costume so that it looks really happy to be getting an answer. If it is an err value, then Happy remains real serious and says "Oops, I can't calculate this!"
|Happy saying the answer|