Week 2

Coke Machine

A loop that keeps asking for a coin and adds only the coins it accepts.

You type

Coin values, one at a time, until the amount is covered.

The tool
Keep asking for a coin Ask for a coin Add it, or ignore it again
What comes out

The amount still owed after each coin, then the change.

The question, more slowly

The question is: a machine is owed 50 cents. You keep asking for a coin. Only some coin values count. After each accepted coin you say how much is still owed. When the person has paid enough, you say the change, which may be zero.

This is a loop with a remaining amount. A coin you do not accept should not change the amount, and you should say so in the way the spec asks. The loop ends when the remaining amount is zero or less.

People end the loop as soon as any coin is typed, or they give change before the debt is covered. Walk 25, then 10, then 25 on paper.

Before you code

Pick an amount owed and three coins on paper. Cross out any coin the machine should refuse. The spec says which coins count.