←Previous page

11CLEAN CODE · DON'T REPEAT

Write it once, use it often.

When lines repeat, a loop or a function can do the work: same output, shorter and safer code.

IN REAL LIFE

Do it once, use it again.

People find smart ways to avoid doing the same work twice.

A song sheet

The chorus is written once. Then the sheet just says "Chorus" again.

A teacher's stamp

She doesn't draw each star by hand. One stamp makes the same star every time.

A pancake recipe

"Repeat for each pancake" instead of writing the steps ten times.

LOOK FIRST

Spot it, then clean it.

Same lines? Loop

Three print("Hooray!") lines become one for loop with range(3).

Small changes? Function

When only a name changes, write a function with a parameter for it.

Fix one place

The lines live in one spot. Change them once and every use changes.

LOOK FIRST

Read the clean version.

Two welcomes, but the lines are written only once.

def welcome(name):One function. The parameter name is the part that changes.
print("Hi, " + name + "!")Written once, used for everyone.
print("Welcome to the team.")Change this line once, and every welcome changes.
welcome("Ana")Runs both lines with name = "Ana".
welcome("Leo")The same lines again, now for Leo.

RUN IT

PyBot cheers for the team.

Guess first. Then run real Python and check.

1

Guess the result

def cheer(name): print("Go, " + name + "!") for friend in ["Ana", "Leo", "Mia"]: cheer(friend)

What will Python show?

cheer.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 "Go, " to "Yay, " in one place and run. All three lines change!

YOUR TURN

Spot the repeat.

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

0Completed
9Not tried
0Review
01

Like a song

A song has the same chorus 3 times.

What is the cleanest song sheet?

02

Same lines

print("Brush teeth") print("Brush teeth") print("Brush teeth")

Which cleaner code shows the same output?

03

Inside the loop

for i in range(4): print("Jump!") print("Rest")

How many repeated print lines does this loop replace?

04

Only the name changes

print("Happy birthday, Ana!") print("Happy birthday, Leo!") print("Happy birthday, Mia!")

What is the best tool to clean this up?

05

Call the function

def birthday(name): print("Happy birthday, " + name + "!") birthday("Leo")

What will Python show?

06

One fix

A greeting lives inside one function. The program calls it 5 times. Now the greeting must change.

How many places do you edit?

07

Why clean it?

Messy code and clean code show exactly the same output.

So why clean it?

08

Fix PyBot's code

PyBot changed the snack from cookies to apples, but missed one copy. Write a function so the snack lives in one place, and match the goal.

Goal output
Ana gets an apple.
Thanks, PyBot!
Leo gets an apple.
Thanks, PyBot!
Mia gets an apple.
Thanks, PyBot!
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Don't Repeat Yourself.

Repeated lines become a loop. Repeated chunks with small changes become a function with a parameter. Then one fix fixes everything.