Home / Hands-on activities / Magic Box: Buttons & What They Do

Coding activity

Magic Box: Buttons & What They Do

Draw a 'magic box' with 3 buttons. For EACH button, write down what HAPPENS when you press it. That mapping — input → output — is what code does!

Hands OnAbout 45 minutesScreen-freeParent preview recommended

Materials and setup

How this changes by age

Pre-K (ages 3–4)

Draw a 'magic box' with 3 buttons. For EACH button, write down what HAPPENS when you press it. That mapping — input → output — is what code does!

Difficulty 1 of 3

Steps

  1. Draw a big rectangle on paper — this is your magic box.
  2. Draw 3 buttons. Number them 1, 2, 3.
  3. Next to the box, write a list: 'Button 1: makes ice cream. Button 2: calls grandma. Button 3: turns lights rainbow.'
  4. Tell a parent: 'When I press THIS, THIS happens.' Practice the words IF and THEN: 'If I press button 1, then ice cream comes out.'
  5. Add a 4th button. Decide what it does — then add it to your list. Notice: you cannot have a button without an action!
  6. Tell a grown-up one thing that surprised you.

Learning objectives

  • Map each input (button) to a specific output (action)
  • Practice IF/THEN logic in plain language
  • Understand that every interactive element needs a defined behavior

Kindergarten (ages 5–6)

Build a magic-box logic table: list every button on the LEFT and what happens on the RIGHT. Add CONDITIONS: 'If it is night, the button does X. If it is day, it does Y.'

Difficulty 2 of 3

Steps

  1. Draw a box with 5 buttons. Label each button with a name.
  2. Make a 2-column table on a separate paper: BUTTON | WHAT HAPPENS.
  3. Fill in the table for every button.
  4. Add a CONDITION to one button: 'If it is night, button 3 turns on the nightlight. If it is day, button 3 plays a song.'
  5. Test your logic: pretend it is night. Read the table and act out what each button does. Then pretend it is day. Different result!
  6. Tell a grown-up one thing that surprised you.

Learning objectives

  • Build an input/output mapping table
  • Introduce CONDITIONAL logic ('if X then Y else Z')
  • Test that logic produces different outputs for different conditions

Early elementary (ages 6–8)

Write the BEHAVIOR of every button as if you were programming it. Use IF/THEN/ELSE. Add state: a counter that goes up each time a button is pressed.

Difficulty 2 of 3

Steps

  1. Draw a magic box with 4 buttons. Make a behavior list, one button per row.
  2. For each button, write the behavior in IF/THEN format: 'IF button A pressed AND counter is even, THEN play happy sound. ELSE play sad sound.'
  3. Add a STATE variable: a counter starting at 0. One button increments it (+1), one decrements it (-1).
  4. Trace through a sequence of presses: start at counter=0. Press +1 three times. What is the counter? What sound plays now?
  5. Trade with a family member: they 'press' buttons in any order, you read the table and tell them what the box does.
  6. In one sentence, tell a parent or sibling what surprised you today.

Learning objectives

  • Write button behavior using IF/THEN/ELSE pseudocode
  • Introduce STATE (a value the program remembers)
  • Trace program execution by hand through multiple inputs

Upper elementary (ages 8–10)

Specify the magic box like a real API: every button is a function with inputs, outputs, and side effects. Write pseudocode and trace edge cases.

Difficulty 3 of 3

Steps

  1. Pick a purpose for the magic box (e.g., a vending machine, a game controller, a smart light).
  2. List every button as a FUNCTION: name, input parameter (if any), output, and side effect (what state changes).
  3. Example: function pressGreen(): if (coins >= 5) { dispense(soda); coins -= 5; return 'success'; } else { return 'need more coins'; }.
  4. Write pseudocode for at least 4 button functions. Use brackets, conditionals, and state variables.
  5. Run edge-case traces: 'What if the user presses Green with 0 coins? With 5? With 100?' Make sure your code does the right thing in every case.
  6. Find ONE bug in your spec by tracing through edge cases. Fix it. Mark what you changed.
  7. In one sentence, tell a parent or sibling what surprised you today.

Learning objectives

  • Specify button behaviors as functions with inputs, outputs, and side effects
  • Write pseudocode using conditionals and state
  • Identify and fix logic bugs by tracing edge cases

Safety and evidence note

Read the full activity before beginning. An adult should supervise tools, heat, food, outdoor work, movement, and experiments as appropriate. Completion records that the activity was done; the child’s explanation, work sample, photo, or demonstration is stronger evidence of learning than a completion check alone.

Related coding activities

Browse all 575 activities · Explore course syllabi