Leçon 1, Chapitre 1
En cours

Initiation au code – Défis add ons !

Yann KIDSHAKER 18 mars 2025

Putting Skills at Work!

With the ingredients in hands, and a little bit or garnish, let’s make a simple dish that adds two numbers; this time, we’ll make the sprite ask the user to give it two numbers. The sprite will then tell the answer.

Let’s get going:

  1. First of all, open a new project. Then, start by making three variables – Number1, Number2, and Result.Number 2Number 1 result
  2. This time, instead of using the when () key pressed block, let’s use the when ‘flag’ clicked block. Go to the Control palette and drag and drop it in the scripting area.
  3. Now, let’s make Tobi first greet the user before anything else. From the Looks palette drag and drop the say () for () secs block below the when ‘flag’ clicked block. We’re not using the say () block because we want that whatever Tobi says, it stays on the stage for a few seconds. First, use a say () for () secs block to allow Tobi to greet and introduce himself and then begin the activity.
    Hi I am TobiChange the number of seconds as you want to. Refer the figure below:
    Sum 1
  4. Then, using the ask () and wait block, ask the user to enter the first number. When the user enters the number, it gets stored in the variable answer.
  5. After asking the user for the first, store this number in the variable Number1. For this, go to the Data&Blocks palette and drag and drop the set () to () block below the ask () and wait block. Then, select Number1 from the drop-down menu. In the second space, drag and drop the answer block. The variable Number1 now has the same value as the answer.
    Sum 2
    Why do we need to do that if the answer variable already has the value? If you open the Sensing palette and observe, there is only one variable to store the value, as you can see below the ask () and wait block, there is only. So, if we use this block again to ask the user to enter the input, the new value will be stored in the variable answer, and the previous value will be lost. This is why we to store the previous value somewhere so that we don’t lose it in the future. Let’s get back to our recipe.
  6. After storing the first number in Number1, ask the user to enter the second number in the same way as done in step 4, and store this number in Number2.
  7. Now that we have the two numbers, Tobi must add them, and he needs your help to do the work. So, without wasting any time, go to the Data&Blocks palette and drag and drop the set () to () block below the previous set () to () block; select Result from the drop-down menu and in the second space drag the drop the () + ­­() block.
  8. In the first space of the () + ­­() block, drag and drop the Number1 variable block, and in the second, drag and drop the Number2 variable block.
    Sum 3
  9. Next, go to the Looks palette and drag and drop the say () for () secs block.
    While writing in the space in this block, you must keep in mind that you don’t know what numbers the user will enter; therefore, you also don’t know the sum. So, how will Tobi tell the answer?! Relax. There is a very simple solution to this problem: Come in the Operatorspalette! In this palette, there block named join () (). This block comes in handy when you want to join different bits and pieces to make a sentence e.g. in your script, you must write a sentence for Tobi such that, every time a user enters different numbers, he is able to tell the correct sum. To do this, you can use the join () () block. How?
  10. First, drag and drop the join () () block anywhere in the scripting area. You’ll notice that there are two spaces in this block. In the first, write The sum of, and in the second, drag and drop another join () () block.
  11. In the second join () () block, drag and drop the Number1 variable block in the first space, and in the second, drag and drop yet another join () () block.
    Result
  12. In the third join () () block, write ‘and’ in the first space, and in the second… yes, drag and drop another join () ()block (You’re getting the hang of it now, aren’t you?)
  13. In the fourth join () () block, drag and drop the Number2 variable block in the first space. You know what to do with the second! (This is the last one, promise!)
  14. In the fifth and final join () () block, writing is in the first space, and this time, in the second space, drag and drop the Result variable block (Told you, this was the last one!).
  15. Now, drag and drop the say () for () secs block below the script you’ve written till now, and the entire combination of join () () blocks made from step 10 to 14 inside the say () for () secs. Here as well change the number of seconds to as much as you want it to be.

And you’re finally done! Every time the green flag is clicked, Tobi will greet you, and ask you or any other user to enter the numbers and will tell the sum accordingly.

Below is the complete script:

Activity Sum of 2 numbers

Sum of 2 numbers

Assignment 2.1

Before you move on to the next topic, 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:

  1. Click on Choose File.
  2. Select the image from the pop-up window that opens up.
  3. Once the image is selected, click on Upload Assignment.
evive Alert
File types allowed is SB3 file generated from the Scratch program. The maximum file size allowed is 5 MB.

Good luck!