Loops: The Lazy Way to Draw


Welcome to Week 2! Last week, drawing a hexagon meant typing forward and right six times each — twelve lines for one shape. Good programmers hate repeating themselves. Today you learn the single most useful idea in all of programming: the loop.

Tell Python to repeat

A loop says “do this, a bunch of times.” Here’s the shape of it:

for step in range(4):
    # the indented lines happen 4 times

Two things to notice:

  • range(4) means do it 4 times.
  • The repeated lines are indented (pushed in). The indentation is how Python knows what’s “inside” the loop.

Here’s a whole square. The two lines you used to repeat are now written once, inside a loop:

 

Same square. The dozen lines became two. That’s the magic: write it once, tell Python how many times.

Try it 🎯

  1. Change range(4) to range(3) and right(90) to right(120). A triangle!
  2. Change to range(5) and right(72). A pentagon!
  3. Change forward(100) to forward(160). Bigger.

The shape rule, now with loops

Remember the rule from Lesson 2? Turn = 360 ÷ number of sides. A loop makes it effortless — you only change two numbers: the count in range(...) and the turn.

Shaperangeturn
Triangle3120
Square490
Pentagon572
Hexagon660
Octagon845

Your turn 🎯

Make a hexagon (6 sides). Change the range number and the turn to match the table:

 

Predict it 🔮

Using the table: what shape does range(8) with right(45) make? Guess first, then Run:

 

(An octagon — 8 sides, like a stop sign.)

Fix the bug 🐞

This loop is supposed to draw a triangle, but it makes a weird open shape. The range is right for a triangle, but the turn is wrong. Fix it using the rule:

 

(Three sides need a turn of 360 ÷ 3 = 120. Change right(90) to right(120).)

Sneaking up on a circle

What if you use lots of sides with tiny turns? Try 36 little steps, turning just 10 each:

 

Almost a perfect circle! A circle is really just a shape with so many tiny sides you can’t see the corners. (36 turns of 10 = 360, a full spin.)

Mix it up 🎨

In the near-circle above, try range(60) with t.right(6) (even smoother), or range(18) with t.right(20) (chunkier). What’s the smallest number of sides that still looks round to you?

Your mission 🚀

Draw a many-sided shape of your choice — anything from a triangle to a 12-sided shape. Pick your number of sides, use the rule (360 ÷ sides) for the turn, and set it up in a loop:

 

What you learned today

  • A for ... in range(n): loop repeats the indented lines n times.
  • Indentation marks what’s inside the loop.
  • Any equal-sided shape: count in range, turn = 360 ÷ sides.
  • Many tiny sides make a circle.

Loops don’t just save typing — they make brand-new things possible. Next time you’ll spin loops into spirals, stars, and flower-like patterns that would be almost impossible to type by hand. 🐢

Comments