←Previous page

10THINK LIKE A PROGRAMMER Β· PLAN

Plan first, then code.

Write the steps in plain words as # comments. Then turn each one into Python.

IN REAL LIFE

Plans come before doing.

People plan in plain words before they start.

A recipe

Cooks read the steps and get the ingredients ready before they turn on the stove.

A trip

Before a trip, your family looks at the map and picks the way.

A school project

You write a list: find pictures, glue them, write the title. Then you start.

LOOK FIRST

How to plan a program.

Input, work, output

What comes in? What do you do with it? What comes out?

Plan in # comments

Write each step in plain words after a #. Python skips those lines.

Pick a tool

For each step, choose a box, an if, a loop, or a function.

Pick the tool: keep something β†’ box =. Choose β†’ if. Repeat β†’ loop. Use the same steps many times β†’ function def.

LOOK FIRST

From plan to Python.

Each step of the plan becomes one line of Python.

cookies = 12Plan: keep the number of cookies. Input, so a box.
friends = 4Plan: keep the number of friends. Another box.
each = cookies // friendsPlan: share them out. The work: // divides into whole cookies.
print("Each friend gets", each)Plan: show the answer. The output.

RUN IT

A plan with its code.

Guess first. Then run real Python and check.

1

Guess the result

# Plan: share cookies # 1. Keep the numbers (input) cookies = 12 friends = 4 # 2. Share them out (work) each = cookies // friends # 3. Show it (output) print("Each friend gets", each)

What will Python show?

cookies.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 friends to 6 and run again. Which step of the plan changed?

YOUR TURN

Plan it, then code it.

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

0Completed
11Not tried
0Review
01

Like a cook

You want to make pancakes for the first time.

What is the best first step?

02

A plan line

# 2. Add the points

What does Python do with this line?

03

Input, work, output

A program: keep your age, add 1, show your age next year.

Which part is the work?

04

Pick the tool

# If it rains, say: # take an umbrella

Which tool fits this step?

05

Pick the tool again

# Say hi to each # friend in the list

Which tool fits this step?

06

Same steps, many places

# Show the game title # at the start, # after a win, # and at the end

Which tool fits this step?

07

Plan to code

# 3. Show the total

Which line of Python does this step?

08

Plan in order

Plan for a program that tells you how many legs 3 dogs have.

Which plan order works?

09

Turn the plan into code

PyBot wrote the plan, but steps 2 and 3 have no code yet. Write one line under each.

Goal output
Total points: 12
OUTPUT

10

Use the tool from the plan

The plan says to greet each friend, but the code only greets one. Use a loop.

Goal output
Hi Ana
Hi Leo
Hi Mia
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Plan in words, then code.

Find the input, the work, and the output. Write each step as a # comment, then turn it into Python with the right tool.