Learning Goals 3 min
By the end of this lesson you will be able to:
- Use ask [What is your name?] and wait to make a sprite show a question box on the Stage and pause every script on that sprite until the user types a reply and presses Enter.
- Read the user's reply back from the (answer) round reporter — and explain that only the most recent reply is stored; the next ask overwrites it.
- Combine ask + (answer) + join [Hello, ] (answer) to make a sprite greet the user by name — your first real input-and-output script.
Warm-Up — three input devices, one missing 7 min
So far in this cluster you've learned to sense three input devices: timers, mouse position, and the keyboard. Each one is great for games — quick, twitch-based reactions. But none of them let your sprite ask a real question and get a real answer.
when flag clicked
say [What's your name?] for (2) seconds
say [Hi!] for (2) seconds
Imagine you click the flag. What does the cat do? What does the cat not do?
Reveal the answer
The cat shows the question for 2 seconds, then says "Hi!" no matter what. It never gives you anywhere to type. There's no input box. The cat is just pretending to ask — it's saying the words but not listening. Useless for a real chatbot, quiz, or name greeter.
Today's lesson is the block that fixes this gap. ask [What is your name?] and wait shows the question and pops up a real text-input bar at the bottom of the Stage and stops everything until the user types something. It's the first block you've met that genuinely listens.
New Concept — ask, wait, answer 15 min
Open the light-blue Sensing palette. Near the top, you'll find a block that looks different from anything you've seen: it has a hat-like curve at the top of the speech-bubble icon, plus a text input.
The block: ask [...] and wait
ask [What is your name?] and wait
When this block runs, three things happen at once:
- The sprite shows the question in a speech bubble on the Stage.
- A grey input box with a tick button appears at the bottom of the Stage.
- Every script on this sprite freezes until the user types a reply and either clicks the tick or presses Enter.
The "and wait" in the block's name is doing real work. It is not "show the question and keep going" — it is "show the question and stop right here until I hear back". This is brand new for you: a block that pauses on purpose.
The reporter: (answer)
Just below the ask block, you'll see a small round reporter:
(answer)
(answer) is a single shared box. There's only one. Every sprite reads the same (answer). And every time any sprite runs an ask, the previous answer is wiped and replaced.
If you want to keep the user's name and ask a second question, you have to save the first answer into a variable before the next ask. We'll do that in L02-19. For today, one ask at a time.
The classic greeting
The most common use of ask-and-wait is greeting the user by name. We need one more block: join [apple] [banana] from the green Operators palette. It glues two pieces of text together into one.
when flag clicked
ask [What is your name?] and wait
say (join [Hello, ] (answer)) for (2) seconds
Notice the empty-looking gap between "Hello," and the comma — that's a real space character inside the square brackets. If you forget it, the cat will say "Hello,Aisyah" with no breathing room. Tiny detail, big difference in how friendly the cat feels.
Using (answer) inside a condition
Because (answer) is just a value, you can compare it to other text or numbers using () = (). This turns ask-and-wait into a quiz:
when flag clicked
ask [What is 7 + 5?] and wait
if <(answer) = [12]> then
say [Correct!] for (2) seconds
else
say [Try again.] for (2) seconds
end
Worked Example — Aisyah's bilingual greeter 12 min
We'll make the cat-pet ask the user's name and their preferred language, then greet them in that language. Eight steps.
Step 1 — New project
Fresh Scratch project. Default cat. Drag it slightly above centre so the speech bubbles don't cover its face.
Step 2 — Drop the flag hat
From Events: when ⚑ clicked.
Step 3 — Ask the name
From Sensing: ask [What is your name?] and wait. The default text is already a name question — perfect. Click the flag to test. An input box appears at the bottom of the Stage. Type a name, press Enter. The box vanishes.
Step 4 — Save the name into a variable
From Variables: click Make a Variable, name it name, click OK. Drag set [name v] to (0) under the ask block, and drop (answer) into the round slot, replacing the 0. Now the cat remembers the name even after the next ask overwrites the global answer.
Step 5 — Ask the language
Another ask [What is your name?] and wait. Change the question text to English or BM?. Snap it under the set-variable block.
Step 6 — Branch on the language
From Control: if <> then else. Drop it under the language ask. From Operators: () = (). Put it in the diamond. Drop (answer) in the left slot and type BM in the right slot.
Step 7 — Fill in the English branch (else)
From Looks: say [Hello!] for (2) seconds. Drop it inside the else half. Replace [Hello!] with a join [Hello, ] [name] (drag the (name) variable into the second slot — remember the trailing space).
Step 8 — Fill in the BM branch (if)
Same idea in the if half. Drop a say [Hello!] for (2) seconds, but use join [Selamat datang, ] (name) in the text slot. Click the flag. Type "Aisyah" → "BM" → the cat says "Selamat datang, Aisyah". Run again. Type "Daniel" → "English" → "Hello, Daniel". You just built a tiny bilingual greeter.
The full assembled stack
when flag clicked
ask [What is your name?] and wait
set [name v] to (answer)
ask [English or BM?] and wait
if <(answer) = [BM]> then
say (join [Selamat datang, ] (name)) for (2) seconds
else
say (join [Hello, ] (name)) for (2) seconds
end
What you just built: the input/output skeleton of every chatbot, quiz app, choose-your-adventure story, and conversational tutorial ever written. The pattern is always the same — ask, save the reply, branch on the reply, respond. L02-19 will turn this into a full Q&A bot in both Bahasa and English.
Try It Yourself — three ask-and-wait drills 15 min
Goal: Ask the user their favourite kuih and have the cat echo it back. Use a single ask and a single say with a join.
when flag clicked
ask [What's your favourite kuih?] and wait
say (join [I love ] (answer)) for (2) seconds
Think: What does the cat say if you press Enter without typing anything? Try it. (Answer: it says "I love " with a trailing space — because (answer) is just empty text, not zero.)
Goal: Build a one-question math quiz. Ask What is 8 × 7?. If the answer is exactly 56, say "Correct! High five!" for 2 seconds; otherwise say "Nope — the answer was 56." for 2 seconds.
when flag clicked
ask [What is 8 × 7?] and wait
if <(answer) = [56]> then
say [Correct! High five!] for (2) seconds
else
say [Nope — the answer was 56.] for (2) seconds
end
Think: What happens if a user types "fifty-six" instead of "56"? What about "56 " with a trailing space? Try both. This is why grown-up quiz software has to do a lot of work to handle "almost-right" answers.
Goal: "Priya's two-step quiz." Ask two math questions in a row. After the first, save the answer into a variable called q1. After the second, save into q2. Then say "You got X correct out of 2." where X is the count of correct answers. (Hint: use two if-thens that each change [score v] by (1), with score reset to 0 at the start.)
when flag clicked
set [score v] to (0)
ask [What is 6 + 9?] and wait
if <(answer) = [15]> then
change [score v] by (1)
end
ask [What is 4 × 5?] and wait
if <(answer) = [20]> then
change [score v] by (1)
end
say (join [You got ] (join (score) [ out of 2.])) for (3) seconds
Think: Why do we set score to 0 at the start? (Hint: what would happen on the second flag click if we didn't?) Variables remember their values between runs — resetting is your job.
Mini-Challenge — Daniel's forgetful chatbot 5 min
"Why is the cat saying the wrong name?"
Daniel wants a cat that asks the user's name, then asks their age, then greets them with both. He writes:
when flag clicked
ask [What is your name?] and wait
ask [How old are you?] and wait
say (join [Hello ] (answer)) for (2) seconds
say (join [You are ] (join (answer) [ years old.])) for (3) seconds
The greeting is wrong — the cat is greeting the age instead of the name. Why?
Reveal one valid solution
The bug is that (answer) is a single shared box that gets overwritten on every ask. By the time Daniel says "Hello ", the box no longer holds "Aisyah" — it holds "10" (the most recent answer, from the age question).
The fix is to save the first answer into a variable before running the second ask. Then we can refer back to it as long as we like:
when flag clicked
ask [What is your name?] and wait
set [name v] to (answer)
ask [How old are you?] and wait
set [age v] to (answer)
say (join [Hello ] (name)) for (2) seconds
say (join [You are ] (join (age) [ years old.])) for (3) seconds
Now name and age each live in their own labelled box. (answer) can change all it likes — the variables don't budge until you set them again.
The lesson: (answer) is a passing whisper, not a memory. If you want to remember an answer past the next ask, copy it into a variable straight away. This habit will save you hours of debugging on quizzes, forms, and chatbots.
Recap 3 min
You met the block that lets a sprite genuinely listen. ask [What is your name?] and wait shows a question, opens a text-input box on the Stage, and freezes every script on the asking sprite until the user types and presses Enter. The reply is stored in the global (answer) reporter — a single shared box that's overwritten by the next ask, on any sprite. The fix for that is to copy important answers into named variables. Combine ask + answer + join + if <> then else and you have everything you need for greetings, quizzes, name-based menus, and the start of a real chatbot.
- Ask-and-wait
- The ask [...] and wait block. Shows a question, opens an input box, and pauses every script on this sprite until the user submits a reply.
- Answer
- The global round reporter (answer) that holds the most recent reply from any ask, on any sprite. There is only one (answer).
- Overwrite
- When new data replaces old data in the same storage box. Every new ask overwrites the previous (answer). Variables don't overwrite themselves — you have to set them on purpose.
- Join (concatenate)
- The join [Hello, ] (answer) operator. Glues two pieces of text into one. You can nest joins to glue three or more pieces.
- Text vs. number
- (answer) is always text, even if the user types "42". Scratch quietly converts when you compare with () = (), so quiz answers usually work — but tiny formatting differences (spaces, decimals) can trip you up.
Homework 2 min
Mamak Order Bot. Build a tiny "Mamak waiter" sprite that takes a teh-tarik order.
- New project. Add any sprite — a person, a cat with a tray, or paint your own waiter.
- when ⚑ clicked → say [Welcome to Mamak Aljay!] for (2) seconds.
- ask [What's your name?] and wait → set [name v] to (answer).
- ask [Teh tarik or kopi?] and wait → set [drink v] to (answer).
- ask [How many?] and wait → set [count v] to (answer).
- Final line: say (join (name) (join [, your ] (join (count) (join [ ] (join (drink) [ is on the way!]))))) for (4) seconds. (Yes, that's a stack of nested joins — build it from the inside out.)
Save as HW-L2-18-Mamak-Order.sb3. Click the flag. Type "Aisyah" → "teh tarik" → "2". The waiter should say "Aisyah, your 2 teh tarik is on the way!".
Bring back next class:
- The
.sb3file. - Your answer to: "Try removing all three set blocks (so the cat never saves anything into variables). Run the script. What does the final say sentence look like? Why?"
Heads up for next class: SCR-L02-19 takes today's tools and builds a real Q&A bot that switches between Bahasa Malaysia and English depending on what the user types. Two languages, one cat, three asks.