Put a coin in, press a button, and a snack drops out. How does the machine know what to do? It doesn't know anything: a person wrote rules for it, and it follows them exactly, every time.
Playing vending machine at home turns those rules into a game. One person plays the machine and is only allowed to do what the rule sheet says. The others are customers and programmers. It suits children of about 7 to 10, and the most useful part comes when customers do strange things nobody planned for.
Build the machine
You need very little:
- A cardboard box with a slot cut in the front for coins and a flap cut at the bottom where items come out. The person playing the machine sits behind it.
- Buttons: three circles drawn on the box, one above each item. You can stick a small picture on each one.
- Items to sell: stickers, crayons, crackers or small toys you already own.
- Coins: play coins or paper circles. Start with coins that are all worth one. Later add a bigger coin worth two.
- A price board: dots under each button, one dot for each coin the item costs. Children who don't read yet can still use it.
- Two dishes behind the box: one for "money put in so far" and one for the machine's own coins, used for giving change.
If a younger brother or sister is around, keep coins and small items out of their reach.
Write the first rules
Before the machine opens, the programmers write the rules on a sheet of paper. Each rule has the same shape: "when… then…". The machine reads the rules from top to bottom and follows the first one that fits.
| When… | Then… |
|---|---|
| a coin comes in | put it in the "money so far" dish |
| a button is pressed and there is enough money | drop that item through the flap and move the money into the machine's coins |
| a button is pressed and there isn't enough money | hold up the empty hand: nothing happens |
Then open for business. If a customer asks "can I have it for one coin less?", the machine says nothing, because no rule talks about that. That is the heart of the game: the machine has no common sense, only rules.
Test the strange cases
People who build real machines try unexpected things on purpose before the machine goes out. Take turns being a tricky customer:
- Not enough money: put in one coin for an item that costs three, then press the button. Then put in two more. Does the machine sell it now?
- Sold out: buy the last crayon, then press its button again with enough money. What does the machine do with the coins?
- Two buttons at once: press two buttons together. Which item drops out, or do both?
- Changing your mind: put in coins, then decide not to buy. Is there a way to get the money back?
- Not a coin: push a button or a bottle cap through the slot.
Each time the machine gets stuck, or does something silly like keeping the coins and giving nothing, ask your child: "Is the machine wrong, or is a rule missing?" Then add or change one rule and test again. The rule sheet grows, for example:
| When… | Then… |
|---|---|
| something that isn't a coin comes in | push it back out through the flap |
| the "give back" button is pressed | give back everything in the "money so far" dish |
| a button is pressed and that item is sold out | do nothing and keep the money in the "money so far" dish |
| two buttons are pressed at once | do nothing |
A good last question: what could a customer do that no rule covers yet? Programmers ask this all the time.
Giving change
Now add the coin worth two. A customer may put in more than the price, so the machine needs a rule for change: give back the money put in, minus the price. Work a few out on paper first:
| Price (dots) | Coins put in | Money put in | Change |
|---|---|---|---|
| 3 | 2 + 2 | 4 | 1 |
| 4 | 2 + 1 + 1 | 4 | 0 |
| 2 | 2 + 2 + 1 | 5 | 3 |
| 5 | 2 + 2 + 2 | 6 | 1 |
| 1 | 2 + 2 | 4 | 3 |
Change of 3 can be given as three coins of one, or as a two and a one. Ask your child which is better for the machine, and why. Many children suggest the rule real machines often use: give the biggest coin that still fits, then the next one, until nothing is left to give back.
Then the trickiest case: what if the machine has run out of coins of one and the change is 1? Your child might decide to give back all the money and not sell, or to draw a sign on the box saying that only the exact money works today. Either is a fine rule, as long as it is written down before the machine runs.
Why this is programming
Without a screen, your child has used several big ideas from coding:
- Conditions: each rule only acts "when" something is true.
- Memory: the "money so far" dish is what the machine remembers between one coin and the next. In a computer this is called a variable.
- Order: the machine follows the first rule that fits, so moving a rule up or down can change what happens.
- Testing every case: a program is only finished when it does something sensible for every customer, including the strange ones.
Adapting the game by age
Ages here are rough guides; every child is different.
- About 7–8: coins worth one only, three rules, and a parent as the machine for the first round.
- About 9–10: add the coin worth two and change. Your child writes the rule sheet alone and plays the machine.
- Older children: add a display, a small card the machine holds up to show the money so far, or limit the sheet to as few rules as possible.
Working out a machine's rule from what goes in and what comes out is also the idea behind "Changing machine" puzzles in the Patterns topic in Dologi: each row shows a number going in and a number coming out, and children find the one change that works for every row.
In short
- A cardboard box, play coins and a price board in dots are enough to build a vending machine.
- The person playing the machine does only what the "when… then…" rules say, read from top to bottom.
- Trying strange cases (not enough money, sold out, two buttons, not a coin) shows which rules are missing.
- Change is the money put in minus the price, and the machine needs a rule for when it runs out of coins.
Read more: The human robot game: screen-free coding with "when… then…" rules
