A learning resource fromSpread Robotics Education Foundation

Lesson 09 · 60 minutes · Variables and choices

Ask the Magic 8-ball

Choose a random number, save it, and use a three-way decision to show yes, no, or not sure.

New programming concepts

Variable and else if

Variable

A named place where a program stores information. Here, answer stores 1, 2, or 3.

Else if

A second condition to check when the first condition was false.

Else

The final path when none of the earlier conditions were true.

answer = 3YES ✓else if answer = 2NO ✕elseNOT SURE —

Why store the number? The program must remember the random result while it checks which picture to show.

Try it here

Ask a yes-or-no question

AB

Think of a safe, playful question.

Answer: not sure.

MakeCode steps

Build the three-way choice

  1. 1

    Start a new project

    Name it Magic 8-ball and add on shake.

    Open MakeCode
  2. 2

    Make a variable

    Open Variables, select Make a Variable, and name it answer.

  3. 3

    Save a random result

    Inside on shake, set answer to pick random 1 to 3.

  4. 4

    Add three paths

    Add if / then / else. Use the plus button to add one else if path.

  5. 5

    Build the conditions

    If answer = 3, show Yes. Else if answer = 2, show No.

  6. 6

    Use the final else

    In else, show the Meh face. If the answer is not 3 or 2, it must be 1.

  7. 7

    Trace before testing

    Pretend answer is 2. Point to every block the program checks. Then test with the simulator.

Debug one path at a time

Temporarily set answer to 3, then 2, then 1 instead of using random. Confirm that every path works. Restore pick random 1 to 3 when finished.

For the teacher

A calm 60-minute plan

Recall
Roll a random number from 1 to 3.

Human variable
A student holds a card named answer.

Guided build
Complete one branch at a time.

Trace and debug
Force 3, 2, then 1.

Play and explain
Ask questions, then name the path used.

Keep the questions safe

Use playful, low-stakes questions such as “Will we read a story today?” Remind learners that a random toy cannot predict real events or make important decisions.

Before you finish

Can you explain it?