Find the part of a longer program that does one job, then change only that part.
Programmers read far more code than they write. The first skill is finding the part that does the job you want to change.
This program is longer than any you have met, but it uses only things you know. The skill now is reading: finding which lines do which job.
Make your prediction first. Help opens once you've locked it in.
from turtle import *
speed(0)
colours = ["red", "orange", "pink", "purple"]
stem = 50
head = 10
penup()
goto(-150, -80)
pendown()
for i in range(len(colours)):
# stem
if i % 2 == 0:
pencolor("green")
else:
pencolor("darkgreen")
pensize(3)
left(90)
forward(stem + i * 10)
# flower head
fillcolor(colours[i])
begin_fill()
circle(head)
end_fill()
head = head + 4
# back down, then along to the next flower
penup()
backward(stem + i * 10)
right(90)
forward(70)
pendown()
Read the code, one comment at a time. How tall is the third stem?
stem is 50. How tall is the third stem (when i is 2)?
Run it and compare it with your prediction. Which part is the turtle running right now?
from turtle import *
speed(0)
colours = ["red", "orange", "pink", "purple"]
stem = 50
head = 10
penup()
goto(-150, -80)
pendown()
for i in range(len(colours)):
# stem
if i % 2 == 0:
pencolor("green")
else:
pencolor("darkgreen")
pensize(3)
left(90)
forward(stem + i * 10)
# flower head
fillcolor(colours[i])
begin_fill()
circle(head)
end_fill()
head = head + 4
# back down, then along to the next flower
penup()
backward(stem + i * 10)
right(90)
forward(70)
pendown()
from turtle import *
speed(0)
colours = ["red", "orange", "pink", "purple"]
stem = 50
head = 10
penup()
goto(-150, -80)
pendown()
for i in range(len(colours)):
# stem
if i % 2 == 0:
pencolor("green")
else:
pencolor("darkgreen")
pensize(3)
left(90)
forward(stem + i * 10)
# flower head
fillcolor(colours[i])
begin_fill()
circle(head)
end_fill()
head = head + 4
# back down, then along to the next flower
penup()
backward(stem + i * 10)
right(90)
forward(70)
pendown()
Match each part with its job.
The update is there, but something undoes it.
Match each part of the program with its job.
The flower heads should grow, but all four are the same size. Click the line that stops them growing.
Add two more colours. Everything else should keep working.
Which line holds the list of colours? Add to it.
Six flowers are wider than four. Move the start further left with goto.
Find the part that makes the stems different, and change only that.
You have found parts by their jobs. In Course B, you will give those parts names, so you can reuse them.
Which part of the program makes the stems different heights?
Where it says stem + i * 10, take away the part that uses i.
Add two more flowers that fit on the screen.
Run your program. This ticks when your code does all of the above.
Students read a longer program that combines the counter, a list, an update and a decision, find the lines that do each job, and change one part without breaking the rest. About 35 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Run the garden, then do the Modify tasks yourself. The program is a loop with a long body: that length is what the next lesson fixes.
Knowing which lines to change. Ask what job the new feature is, then which lines do that job.
Stretch: Make every other flower head a different shape by adding a decision to the flower-head lines.
Support: Colour or underline the lines for each job on paper before changing anything.
Next lesson names a group of lines (a function). The garden's long loop body is the first thing to name.