Ed EdisonCode
L3 โ€” Loops & Shapes
1 / 6
Lesson 3 of 6
Loops & Shapes
Using for loops to draw geometric shapes with Edison
Year 9 ยท Robotics with Edisons
1Explain what a for loop is and how range(n) controls the number of repetitions
2Calculate the correct turn angle for any regular polygon using 360 รท sides
3Write and test an EdPy program that makes Edison draw a chosen shape using a for loop
Prior Knowledge: Lesson 2 โ€” Ed.Drive(), Ed.TurnRight(), named variables, downloading to Edison.
Prep: Have markers/pens and paper ready for Edison to draw on (or use tape to mark the floor). Students who finish early can try the spiral extension.
Slide 2 / 6
Key Vocabulary
Iteration and loop terminology
๐Ÿ”„
Iteration
Repeating a block of code. Each repeat is called one iteration. Loops are the programming construct that implements iteration.
๐Ÿ“
For Loop
A loop that repeats a fixed number of times. In Python: for i in range(4): โ€” the indented code runs 4 times.
๐Ÿ”ข
range(n)
Generates a sequence of numbers from 0 to nโˆ’1. range(4) gives 0,1,2,3 โ€” 4 numbers total, so the loop body runs 4 times.
โ†”๏ธ
Indentation
Python uses 4 spaces of indentation to show what code belongs inside the loop. Incorrect indentation causes errors. EdPy (Python) is strict about this.
Analogy: "Imagine writing 'stir the pot' on a recipe 10 times vs writing 'stir 10 times'. The loop is the second version โ€” less writing, same result."
Slide 3 / 6
How For Loops Work
Trace through the execution step by step
sides = 4 side_length = 20 turn_angle = 90 speed = Ed.SPEED_3 for i in range(sides): Ed.Drive(Ed.FORWARD, speed, side_length) Ed.TurnRight(speed, turn_angle)
The indented lines run on EVERY iteration. Lines outside the loop run once, before or after.
i =Action
0Drive forward 20cm โ†’ Turn right 90ยฐ
1Drive forward 20cm โ†’ Turn right 90ยฐ
2Drive forward 20cm โ†’ Turn right 90ยฐ
3Drive forward 20cm โ†’ Turn right 90ยฐ
Loop ends โ€” square complete โœ“
Key question: "i starts at 0, not 1. Does that matter for our shape?" โ€” No, because we only care about the number of iterations, not the value of i.
Slide 4 / 6
Turn Angle Formula
The maths behind drawing any regular polygon
Turn Angle = 360 รท Number of Sides
ShapeSides360 รท sidesTurn Angle
โฌ› Square4360 รท 490ยฐ
๐Ÿ”บ Triangle3360 รท 3120ยฐ
โฌ  Pentagon5360 รท 572ยฐ
โฌก Hexagon6360 รท 660ยฐ
โญ Octagon8360 รท 845ยฐ
import Ed Ed.EdisonVersion = Ed.V3 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM sides = 6 # change this! side_length = 20 turn_angle = 360 // sides # integer division speed = Ed.SPEED_3 for i in range(sides): Ed.Drive(Ed.FORWARD, speed, side_length) Ed.TurnRight(speed, turn_angle) Ed.PlayBeep() # signal completion
// vs /: // is integer (whole number) division. Edison needs whole degrees โ€” 360 / 5 = 72.0 but 360 // 5 = 72.
Slide 5 / 6
Student Task
Code a shape โ€” must NOT be a square
Direct students to: student.html โ†’ L3 Loops & Shapes. Steps 1โ€“3.
1Step 1: Write the square loop example in EdPy. Screenshot and answer: what's the difference between a loop and 4 copied lines?
2Step 2: Study the angle table. Write the generalised code using 360 // sides. Screenshot showing the calculation.
3Step 3: Choose any shape that is NOT a square. Code and test it. A triangle, hexagon, and octagon are all valid.
๐Ÿฅ‰ Bronze
Working loop that draws a non-square shape. Uses range(sides) and 360 // sides.
๐Ÿฅˆ Silver
+ Generalised: changing one variable (sides) should change the whole shape. + Ed.PlayBeep() at end.
๐Ÿฅ‡ Gold
Spiral: add a variable that increases side_length each iteration using side_length += 2 inside the loop.
Physical tip: Tape a marker to the underside of Edison and place it on paper to see the shape drawn. Alternatively, set up a clear path on the floor and watch the turn angles.
Slide 6 / 6
Plenary
Consolidate and preview Lesson 4
Exit Ticket โ€” Mini-Whiteboard / Show of Hands
"I want to draw a regular 12-sided shape (dodecagon). What value do I give sides and what will turn_angle be?"
โœ… Today we covered
  • For loops and range(n)
  • Python indentation rules
  • Turn angle formula: 360 รท sides
  • Integer division with //
  • Drawing shapes with Edison
โญ Next Lesson
Lesson 4: Flowcharts & Obstacle Detection

We will design algorithms using flowcharts, then program Edison to detect and respond to obstacles using IR sensors and if/elif/else.
Exit ticket answer: sides = 12, turn_angle = 360 // 12 = 30ยฐ.
๐Ÿ—’ Notes โ€” Slide 1
๐Ÿ—’ Notes โ€” Slide 2
๐Ÿ—’ Notes โ€” Slide 3
๐Ÿ—’ Notes โ€” Slide 4
๐Ÿ—’ Notes โ€” Slide 5
๐Ÿ—’ Notes โ€” Slide 6