Skip to content

A2 - Loops

Resources to consult in addition to our lesson:

Some More Turtle Functions to Try:

  • t.penup() - stop leaving a trail
  • t.pendown() - start leaving a trail
  • t.goto(x, y) - go to the given coordinates
  • t.teleport(x, y) - teleport to the given coordinates (no trail left even if pen is down)
  • t.circle(radius) - move in the shape of a circle with the given radius

Examples

Some of these examples are included in your assignment file, but there are also a few more contained here. Copy these into your own code one at a time to avoid things getting too crazy!

import turtle
from grid import draw_grid

draw_grid()
t = turtle.Turtle()

# Customizable Setup
t.speed(2)
t.color("green")
t.width(2)

## EXAMPLES START HERE

for i in range(4):
    t.forward(50)
    t.right(90)


for i in range(5):
    t.forward(50)
    t.right(72)


for j in range(10):
    print(j)
    t.forward(j)
    t.right(90)


for k in range(20):
    print(k)
    t.forward(k*10)
    t.right(90)


t.width(8)
t.teleport(-300, 0)
for i in range(6):
    t.pendown()
    t.forward(50)
    t.penup()
    t.forward(50)


t.left(90)
for x in range(0, 300, 50):
    t.goto(x, 0)
    t.forward(50)


t.width(5)
t.left(90)
for x in range(-300, 300, 50):
    t.teleport(x, 0)
    t.forward(x/2)


t.width(1)
t.speed(10)
for x in range(100):
    t.forward(x) # Distance increases with each loop iteration
    t.left(91) # A slight angle to create a spiral


t.speed(10)
for x in range(-200, 201, 100):
    for y in range(-200, 201, 100):
        t.teleport(x, y)
        t.circle(20)


# Keep the window open when done
turtle.done()

Your Task

Run the update to get the examples. Uncomment the examples one at a time to see what they do. Play around with the examples to get a feel for how they work.

When you feel ready, create two or three of your own examples using loops. Feel free to create new file(s) for your own examples.

For extension credit, do more than three examples and/or try some more complex things.

Tip

To comment and uncomment multiple lines in Visual Studio Code, you can highlight the lines and press CTRL+/