Initiation au code – Défis calculatrice
Now the time has come, my child, to gather all the knowledge you have gained and put it to good use. You will get the assistance you need along with your journey. Go ahead, and…
Enough of trying to sound all wise! Let’s roll!
The good news is that the beginning is the same as the script that you wrote for calculating the sum of two numbers, i.e. the part where Tobi introduces himself, talks about his ‘ability’ to calculate the sum of any two numbers, and asks the user to enter any two numbers of her/his choice.
In this activity, you also must ask the user what operation does she/he wants to perform, i.e. addition, subtraction, multiplication or division.
Since you already know the beginning, let’s directly get to asking the user what operation she/he wants to perform. This is the same as asking the user to enter any two numbers; so, here as well you must use the ask () and wait block. Ask the user to choose from the following symbols: ‘+’,’-’, ‘*’, and ‘/’.
- You must store the input you get somewhere, even though it is stored in the variable answer as well. For that create a new variable Operation to store the input for the preferred operation. Then, as done earlier, store the input in Operation by using the set () to () block; you might notice that since the latest variable you created is Operation, it is by default written in the set () to () block; you don’t have to change it.
- Below the set () to () block, using which you store the input in the variable Operation, drag and drop the if block from the Control palette. In the diamond-shaped space, drag and drop the () = () block from the Operators block.
- In the first space of () = () block, drag and drop the Operation variable block, and in the second space write +. Then drag and drop the set () to () block inside the if block; select Result from the first drop-down menu, and in the second space, drag and drop the () + () block.
- In the two spaces of the () + () block, like in the previous activity, drag and drop the Number1, and Number2 variable blocks.
You might have guessed what we’re doing here: The () = () block checks if the input entered is +. If it is, the block inside the if block will be executed; if not, then the computer will move ahead to the next block.

- Similar to what you did in step 4, drag and drop if blocks for the remaining three operations as well. The procedure is the same; in the () = () block, in place of +, write the operator (‘-’, ‘*’, or ‘/’) that you are checking. You’ll find the () – (), () * (), and () / () blocks in the Operators palette.
- After you’ve checked all four conditions, you must complete the script by bringing in the block that will display the result. This part as well is similar to the part of the previous activity; here, instead of five join blocks, you need only four. Why? Because here you don’t have to, rather you can’t begin the sentence by The sum of.
Tada! You’ve successfully made a basic calculator which you can use to do calculations for you.
Assignment 2.2
Before you move on to the next lesson, a small assignment awaits you!
You must upload the Scratch script you wrote in this activity to the website. Submitting the assignment is a must in order to receive the certificate after completing the course.
Submitting the assignment is a must in order to receive the certificate after completing the course.
Follow the steps below to upload your assignment:
- Click on Choose File.
- Select the image from the pop-up window that opens up.
- Once the image is selected, click on Upload Assignment.

Good luck!

