⭐ 1.5.8 Programming with Different Paths

View

The next thing you do is to create three different paths the program can take depending on the user’s answer to the first question (nested condition):

  1. If the user enters +, the program takes the user to the addition quiz.

  2. If the user enters x, the program takes the user to the multiplication quiz.

  3. If the user enters anything other than x or +, Scratch the Cat says that she does not understand.

Activity: Creating Nested Conditions

To program these three paths, follow these steps:

  1. Drag an if () then, else block from the Control Block Palette to the Scripts Area and attach it to the ask () and wait block.

  2. Drag a () = () block from the Operators Block Palette and place it in the hexagram shape in the if () then, else block.

  3. Drag the answer block from the Sensing Block Palette and place it into the left side of the () = () block.

  4. Enter the + symbol into the right side of the () = () block.

  5. Drag an if () then, else block into the else part of the first if () then, else block, as shown in Figure

    1.44.

  6. Drag a () = () block into the new if () then, else block.

  7. Drag an answer block into the left side of the () = () block.

  8. Enter a lowercase x into the right side of the () = () block.

  9. Drag a say () for () secs block from the Looks Block Palette and drop it into the else section of the

    nested if () then, else block.

  10. Change the value in the say () for () secs block to I did not understand your answer.

  11. Your script should now look like Figure 41.

Figure 41: Creating a nested condition

Now you have created the three main branches of the program! Next, you will build the addition quiz.

Activity: Making an Addition Quiz

Follow these steps to create an addition Quiz.

  1. Drag a repeat () block from the Control Block Palette and drop it into the empty space in the first if () then, else block.

  2. Create two new variables in the Data Block Palette: Number1 and Number2.

  3. Drag a set () to () block from the Data Block Palette and snap it inside the repeat () block.

  4. Set the first value in the set () to () block to Number1.

  5. Drag a pick random () to () block from the Operators Block Palette and snap it into the second

    space in the set () to () block.

  6. Change the second value in the pick random () to () block to 100.

  7. Duplicate the set () to () block you just created and snap it to the bottom of the first one.

  8. Change the first value in this new set () to () block to Number2.

  9. Drag an ask () and wait block to the Scripts Area and snap it to the bottom of the set (Number2)

    to (pick random (0) to (100)) block.

  10. Drag a join () () block into the space in the Ask () and wait block.

  11. Drag another join () () block into the second space in the join () () block.

  12. Drag one more join () () block into the second space of the last join () () block.

  13. Make your Ask () and wait block look like Figure 1.46.

  14. Drag an If () then, else block to the Scripts Area and snap it to the bottom of the Ask () and

    wait block.

  15. Drag a () = () block into the space in the If () then, else block.

  16. Drag an answer block into the first part of the () = () block.

  17. Drag a () + () block into the second part of the () = () block.

  18. Snap the Number1 variable into the first space in the () + () and snap the Number2 variable into

    the second space in the () + () block.

  19. Drag a say () for () secs block inside the space after then, in this if () then, else block.

  20. Change the value of this say () for () secs block to Correct!

  21. Drag a change () by () block from the Data Block Palette and snap it to the bottom of the say ()

    for () secs block.

  22. Change the first value in the change () by () block to Score and change the second value to 1.

  23. Drag a say () for () secs block to the Scripts Area and snap it into the else part of the If () then,

    else block.

  24. Change the value of this say () for () secs block to No, that’s not correct.

Figure 42: An addition question

CHALLENGE

Can you create a message that displays after the repeat (10) loop to tell the users how many answers they got correct?

The finished addition quiz, including the answer to the challenge question, is shown in Figure 43.

Figure 43: The final addition quiz


Activity: Making a Multiplication Quiz

The multiplication quiz is very similar to the addition quiz. The only difference lies in the messages that are displayed and in the values. Set the range for the multiplication quiz from 0 to 12, so that it is easier for people taking the quiz to do the math in their heads.

Can you create this part of the program yourself? Here are the main steps:

  1. Drag the repeat () block Repeat loop out of the script for a moment, and then duplicate it and place the duplicate into the space after if (answer = x) then.

  2. Put the original repeat () block back where it was.

  3. Modify the contents of the new repeat () block, as well as the say () for () block that follows it

    and gives the score, to match Figure 1.

Figure 45:The final Math and Multiplication Quiz Project

When you’re done, the whole program should look like Figure 45 above.


Last modified: Friday, 24 September 2021, 4:26 PM