Monday, December 12, 2011

Project: Happy Calculator

Here we are again with another Scratch project! We are going to program a simple calculator. Scratch has built in operators that we can use and we will use the ready-made addition, subtraction, multiplication and division operators.

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 variables

We 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 input

Having set those variables, we program Happy to ask for user input:

Asking for and getting input
The script is pretty straightforward. We use the ask_and wait block and then we set a variable to the answer given by the user.

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
We have a chunk of code here as you can see. They are actually just a lot of nested if-else blocks. I prefer this to snapping many if blocks together, since the whole "else" part is immediately ignored once the "if" condition is met. Basically, what we are doing here is to check the user's input for the operator symbol.

Addition, subtraction and multiplication

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

Addition
Subtraction
Multiplication

Division

When 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
When the divisor is zero, we set the ans variable to "Anything divides zero is undefined!" This makes Happy a pretty smart talking calculator. If the divisor is anything but zero, we do division with Scratch's divide block.

Power

Scratch 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
So, what we do is to first set the ans variable to 1. The we multiply ans by num1, num2 number of times using the repeat block. For example, if num1 is 9 and num2 is 3, then we repeat 3 times the multiplication of ans to 9. At every loop of the repeat block we update ans to the latest multiplication result. Take a look at an example:

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 operator

If 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 answer

By 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

2 comments:

  1. This is a cool way to teach Scratch maths capabilities, and make your own calculator.

    ReplyDelete
  2. The thinking put into the ^ (power) symbol is smart!

    ReplyDelete