In this video, you will make the sprite respond to the user’s input.
In the last video, you added an “ask” block.
The “ask” block works with the "answer" block.
When you use an “ask” block, Scratch saves the user’s response in the “answer” block.
Find the “answer” block in the Sensing menu.
Explore how the “answer” block works by clicking the checkbox next to it.
The answer value will show up in the top left corner of the stage.
Click on the “ask” block, and enter a response.
You should see the text you entered appear in the top left corner of the stage.
Each time you click the “ask” block and enter a response, the “answer” box will update with the new value.
Now program the sprite to check if the answer the user entered is the correct one.
Say something like “You win!” if the user is correct.
To check if something is true, use a “conditional” block.
Computers use conditional statements to make decisions.
Drag out an “if-else” block from the control menu, and snap it below the “ask” block.
Notice the diamond-shaped space in the “if-else” block.
This space is for a block called a “boolean.”
A boolean is a statement that is either true or false.
For this project, the boolean will determine whether the answer your friend entered equals the correct answer.
In the operators menu, find the block that has an equals sign.
Drag it to the scripts editor.
Then, from the sensing menu, drag out the “answer” block, and place it inside the first value of the “equals” block.
Type the letter that matches the “lie” into the second half of the “operator” block.
In this example, the lie is the “C” statement, but you will type in the letter for the statement that is a lie in your project.
To test whether this works correctly, click on the “ask” block in the “sensing” menu.
Type in the letter for your lie statement when you are prompted.
Then, click the green “operator” block.
It says true!
That’s because the “answer” block saved the value you just typed when running the code.
The operator block compared “answer” to the letter C.
Since they are equal, it says “true.”
If you enter a different value, like B, then it will say “false.”
Drag the green operator block into the “if-else” block.
Now, you can read the code like a sentence.
If the answer equals C, the lie, then a winning action should happen!
To program the winning condition, click on the Looks menu, and drag a say block into the if section of the conditional block.
Change the text to something that indicates you picked the lie, like “you win!”
Try it out!
First, the sprite says one true thing.
Then, it says the second true thing.
Then, the sprite says the lie.
Next, it asks the user a question.
In this example, the user enters C. The program checks to see if the answer matches the lie - it does!
So, the sprite says “you win!”
Test your code a few times to make sure it works.
Try entering different values, like A, B, or C.
Now it’s your turn!
Check if the user’s answer is the lie using an “if-else” block and an “equals” operator block.
Program a winning condition by placing a “say” block in the “if-else” block.
- Check if the user's answer is the lie using an "if-else" block and an "equals" operator block.
- Program a winning condition by putting a "say" block in the "if-else" block.