When the same three lines appear in several places, they are one function that was never named. Repeating yourself means changing the same thing in several places.
What you will see
Points: 4
About 2 min of reading
Quick check
Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.
Before you run it, say out loud what it will print. Then open the answer.
What it prints
Points: 4
Put it in order
Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.
Data Flow
Any block that appears more than once should be named once as a function, so a change happens in one place.
1shape repeats
↓
2function holds it once
↓
3calls replace copies
↓
4one edit changes all
Line by line
Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.
print("Score: " + str(4))Score: 4
print("Score: " + str(9))Score: 9
Why this worksOne place to change
The value of a function is that its body exists once. Every rule that appears twice should exist once, so a change is one edit rather than a hunt.
Do not abstract too early
A function used once is a detour. Wait until the copy is real, or the shape is likely to change, before naming it.