Making a sandwich
Get the bread, add the filling, close it, cut it. Four small jobs.
10THINK LIKE A PROGRAMMER · SPLIT IT
A big problem is many small problems together. Solve one small piece at a time.
IN REAL LIFE
You already split big jobs every day.
Get the bread, add the filling, close it, cut it. Four small jobs.
First the toys, then the clothes, then the books. Much easier!
The box has numbered bags. You build bag 1, then bag 2, then bag 3.
LOOK FIRST
Ask: what small jobs make up the big job?
Make one piece work and check it before the next one.
Put the pieces in order. Each piece uses what the one before made.
Remember? At the start of the course you made tiny plans in steps. Now each step becomes Python. Thinking in steps
LOOK FIRST
Each line is one piece of the shop program.
prices = [3, 5, 4]Piece 1: keep the prices in a list.total = total + pricePiece 2: inside a loop, add each price to the total.total = total - 2Piece 3: if the total is 10 or more, take 2 coins off.print("You pay", total, "coins")Piece 4: tell the customer how much to pay.RUN IT
Guess first. Then run real Python and check.
# Piece 1: the prices
prices = [3, 5, 4]
# Piece 2: add them up
total = 0
for price in prices:
total = total + price
# Piece 3: the discount
if total >= 10:
total = total - 2
# Piece 4: the message
print("You pay", total, "coins")What will Python show?
PyBot's hint
Ready. Press Run Python.
Pyodide 314.0.3 runs CPython in your browser with WebAssembly.
Open-source under the Mozilla Public License 2.0. License
Now change one thing: Change the prices to [1, 2, 3] and run again. Does the discount piece still work?
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
Which plan splits the job into pieces?
Why does PyBot split it into pieces?
Which piece must come before adding points?
total = total + priceWhich piece of the shop does this line do?
prices = [2, 6]
for p in prices:
total = total + p
print("You pay", total)NameError: name 'total' is not defined
Which piece is missing before the loop?
What is a smart next step?
What makes a good small piece?
Piece 3 is missing. Big buys of 10 coins or more get 2 coins off. Add it and match the goal.
You pay 9 coinsOUTPUT
The message piece runs too early. Move the pieces so each one comes after the piece it needs.
You pay 7 coinsOUTPUT
Want a fresh start? Clear only this page.
Find the pieces, solve one at a time, check each one, then join them in order.