Building a Simple Calculator in Scratch

Scratch 14-05-25
893 11

Tutorial plan

1- Objective of the tutorial

2- Step-by-Step: Create a Basic Calculator in Scratch

 

 

Objective of the tutorial

The objective of this project is to teach basic programming concepts using the Scratch programming language by creating a simple, interactive calculator. This calculator will allow users to input two numbers and choose an operation (addition, subtraction, multiplication, or division) to perform on those numbers. The result will then be displayed on the screen.

1- User Interaction:

The calculator will ask the user for input through the "ask" block to collect two numbers and an operation (like +, –, ×, ÷).

The program will handle the input in a way that allows the user to enter these values interactively, instead of hardcoding them.

2- Basic Operations:

The calculator will perform basic arithmetic operations on the two numbers based on the user's input:

Addition (+)

Subtraction (-)

Multiplication (×)

Division (÷)

After the operation is performed, the result will be displayed.

3- Output and Display:

The program will output the result of the operation on the screen, either using a "say" block or by displaying the result in a variable that the user can see updated in real-time.

Error Handling (Optional):

The program can be enhanced to handle potential errors such as:

Dividing by zero, which could display a special message.

Invalid input (e.g., non-numeric values or unsupported operations).

 

Step-by-Step: Create a Basic Calculator in Scratch

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.

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.

2) Creation of the addition sprite :

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

Draw the addition sprite using the drawing editor.

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.

3) Creation of the subtraction sprite:

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

Draw the subtraction sprite using the drawing editor.

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.

4) Creating the product sprite:

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

Draw the sprite product using the drawing editor.

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.

4) 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.

11 comments

Image
sung jin-woo solo leveling 04-11-2525

I was recommended this web site by my cousin. I'm not sure whether this post is written by him as nobody else know such detailed about my problem. You're amazing! Thanks!

Image
Read one-punch man 03-11-2525

An outstanding share! I've just forwarded this onto a co-worker who had been conducting a little research on this. And he in fact bought me lunch simply because I discovered it for him... lol. So let me reword this.... Thank YOU for the meal!! But yeah, thanx for spending some time to discuss this topic here on your site.

Image
Saitama One punch man 03-11-2525

Pretty! This has been a really wonderful article. Many thanks for providing this info.

Image
about 03-11-2525

Wonderful blog! Do you have any tips for aspiring writers? I'm hoping to start my own blog soon but I'm a little lost on everything. Would you advise starting with a free platform like Wordpress or go for a paid option? There are so many choices out there that I'm totally confused .. Any tips? Thanks a lot!

Image
read Who Made Me a Princess 31-10-2525

I really like your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz reply as I'm looking to design my own blog and would like to know where u got this from. many thanks

Image
site 31-10-2525

Great post. I was checking continuously this blog and I am impressed! Extremely useful info specifically the last part : ) I care for such information a lot. I was looking for this certain information for a very long time. Thank you and good luck.

Image
read manga Juujika no Rokunin 29-10-2525

I'm truly enjoying the design and layout of your site. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Great work!

Image
Chainsaw Man Movie 29-10-2525

I really like what you guys are up too. This type of clever work and reporting! Keep up the excellent works guys I've added you guys to my own blogroll.

Image
Read manga 27-10-2525

Hiya very cool website!! Guy .. Beautiful .. Superb .. I'll bookmark your blog and take the feeds also? I'm glad to find so many helpful information here in the put up, we need work out extra techniques in this regard, thank you for sharing. . . . . .

Image
site 23-10-2525

Good post. I learn something new and challenging on websites I stumbleupon on a daily basis. It will always be exciting to read through articles from other writers and practice something from their sites.

Image
Readmanga.cc 22-10-2525

Wonderful blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Appreciate it

Leave a comment

Veuillez noter s'il vous plaît*

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Purpose of this website

Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.

Contact details

Zaouiet Kontech-Jemmel-Monastir-Tunisia

+216 92 886 231

medaliprof@gmail.com

Pictures of the articles

Robotic site created by MedAli-Teacher info