←Previous page

10THINK LIKE A PROGRAMMER · SPLIT IT

Split big problems into pieces.

A big problem is many small problems together. Solve one small piece at a time.

IN REAL LIFE

Big jobs are small jobs.

You already split big jobs every day.

Making a sandwich

Get the bread, add the filling, close it, cut it. Four small jobs.

Cleaning your room

First the toys, then the clothes, then the books. Much easier!

A big LEGO set

The box has numbered bags. You build bag 1, then bag 2, then bag 3.

LOOK FIRST

How programmers split a problem.

Find the pieces

Ask: what small jobs make up the big job?

Solve one piece

Make one piece work and check it before the next one.

Join the pieces

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

A tiny shop in four pieces.

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

Run the whole shop.

Guess first. Then run real Python and check.

1

Guess the result

# 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?

shop.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

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

Find the pieces.

Try each tiny question. A wrong answer becomes something to review.

0Completed
10Not tried
0Review
01

Like a LEGO set

You get a LEGO castle with 300 pieces in numbered bags.

Which plan splits the job into pieces?

02

Why split it?

PyBot wants to write a quiz game with questions, points, and a final score.

Why does PyBot split it into pieces?

03

Which piece first?

A game program: start the score, add points for each win, show the score.

Which piece must come before adding points?

04

Name the piece

total = total + price

Which piece of the shop does this line do?

05

The missing piece

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?

06

Check one piece

Your shop shows the wrong total. The prices piece looks fine.

What is a smart next step?

07

A good piece

Pieces of a birthday program: count the guests, buy the cake, send the invitations.

What makes a good small piece?

08

Add the missing piece

Piece 3 is missing. Big buys of 10 coins or more get 2 coins off. Add it and match the goal.

Goal output
You pay 9 coins
OUTPUT

09

Put the pieces in order

The message piece runs too early. Move the pieces so each one comes after the piece it needs.

Goal output
You pay 7 coins
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Big problems are small problems together.

Find the pieces, solve one at a time, check each one, then join them in order.