+21622886281
Scratch

Calculator program with Scratch

Description of the calculator program with Scratch:

The objective of this tutorial is to make a basic calculator that can calculate the sum, subtraction, multiplication, or division of two numbers with the Scratch software.

First of all we will create three variables: a and b to contain the values ​​entered by the user and resulat to contain the result of the mathematical operation (sum or subtraction or multiplication or division)

We will use 5 sprites:

The cat sprite contains the code to enter two numbers

The addition sprite contains the code that calculates the addition of two numbers

The subtarction sprite contains the code that calculates the subtraction of two numbers

The product sprite contains the code to calculate the product of two numbers

The division sprite contains the code that calculates the division of two numbers

1) Creation of the three variables a, b and result:

We must go to the Variables category to create the three variables a, b and result.

2) Program insertion in the code area of ​​the cat sprite:

We start by inserting the green flag event block.

Then we hide the result variable.

Then we ask the user to give the value of the variable a.

The value entered in the predefined response variable is assigned.

We assign the value of the predefined response variable to variable a.

We do the same thing to enter the value of the variable b.

Finally here is the final program of the sprite cat.

This program lets you enter two values ​​from the keyboard and store them respectively in the two variables a and b.

3) Creation of the addition sprite :

1) Go to the new sprites section and select the brush icon.

2) Draw the addition sprite using the drawing editor.

3) Go to the code area of ​​the addition sprite and insert these blocks:

When you click on the addition sprite with the mouse, the program displays the result of the addition in the result variable.

4) Creation of the subtraction sprite:

1) Go to the new sprites section and select the paint icon.

2) Draw the subtraction sprite using the drawing editor.

3) Go to the code area of ​​the subtraction sprite and insert these blocks:

When you click on the subtraction sprite with the mouse, the program displays the result of the subtraction in the result variable.

5) Creating the product sprite:

1) Go to the new sprites section and select the brush icon.

2) Draw the sprite produced using the drawing editor.

3) Go to the code area of ​​the product sprite and insert these blocks:

When you click on the sprite produced with the mouse, the program displays the result of the product in the result variable.

5) Creation of the division sprite:

1) Go to the new sprites section and select the paint icon.

2) Draw the division sprite using the drawing editor.

3) Go to the code area of ​​the division sprite and insert these blocks:

When you click on the sprite produced with the mouse, the program displays the result of the division in the result variable.

To avoid division by 0 which is impossible, we used the block if then to check the value of variable b: if b=0 we display “Division not possible” in the result variable and if b is different from 0, we display the result of the division.




0 commentaire


Leave a comment

Votre adresse e-mail ne sera pas publiée.

Scroll to Top