⭐ 1.5.8 Programming with Different Paths
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):
-
If the user enters +, the program takes the user to the addition quiz.
-
If the user enters x, the program takes the user to the multiplication quiz.
-
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:
-
Drag an if () then, else block from the Control Block Palette to the Scripts Area and attach it to the ask () and wait block.
-
Drag a () = () block from the Operators Block Palette and place it in the hexagram shape in the if () then, else block.
-
Drag the answer block from the Sensing Block Palette and place it into the left side of the () = () block.
-
Enter the + symbol into the right side of the () = () block.
-
Drag an if () then, else block into the else part of the first if () then, else block, as shown in Figure
1.44.
-
Drag a () = () block into the new if () then, else block.
-
Drag an answer block into the left side of the () = () block.
-
Enter a lowercase x into the right side of the () = () block.
-
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.
-
Change the value in the say () for () secs block to I did not understand your answer.
Your script should now look like Figure 41.
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.
-
Drag a repeat () block from the Control Block Palette and drop it into the empty space in the first if () then, else block.
-
Create two new variables in the Data Block Palette: Number1 and Number2.
-
Drag a set () to () block from the Data Block Palette and snap it inside the repeat () block.
-
Set the first value in the set () to () block to Number1.
-
Drag a pick random () to () block from the Operators Block Palette and snap it into the second
space in the set () to () block.
-
Change the second value in the pick random () to () block to 100.
-
Duplicate the set () to () block you just created and snap it to the bottom of the first one.
-
Change the first value in this new set () to () block to Number2.
-
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.
-
Drag a join () () block into the space in the Ask () and wait block.
-
Drag another join () () block into the second space in the join () () block.
-
Drag one more join () () block into the second space of the last join () () block.
-
Make your Ask () and wait block look like Figure 1.46.
-
Drag an If () then, else block to the Scripts Area and snap it to the bottom of the Ask () and
wait block.
-
Drag a () = () block into the space in the If () then, else block.
-
Drag an answer block into the first part of the () = () block.
-
Drag a () + () block into the second part of the () = () block.
-
Snap the Number1 variable into the first space in the () + () and snap the Number2 variable into
the second space in the () + () block.
-
Drag a say () for () secs block inside the space after then, in this if () then, else block.
-
Change the value of this say () for () secs block to Correct!
-
Drag a change () by () block from the Data Block Palette and snap it to the bottom of the say ()
for () secs block.
-
Change the first value in the change () by () block to Score and change the second value to 1.
-
Drag a say () for () secs block to the Scripts Area and snap it into the else part of the If () then,
else block.
-
Change the value of this say () for () secs block to No, that’s not correct.
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.
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:
-
Drag the repeat () block out of the script for a moment, and then duplicate it and place the duplicate into the space after if (answer = x) then.
-
Put the original repeat () block back where it was.
-
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.
When you’re done, the whole program should look like Figure 45 above.