0% found this document useful (0 votes)
12 views15 pages

For Loops For Turtle

Uploaded by

ngaiyishun1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

For Loops For Turtle

Uploaded by

ngaiyishun1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

COMP1021

Introduction to Computer Science

Using For Loops


with Turtle Graphics

Gibson Lam and David Rossiter


Outcomes
• After completing this presentation, you are
expected to be able to:
1. Explain the difference between while loops
and for loops
2. Use for loops to create patterns with graphics
programming
3. Use nested for loops to create patterns with
graphics programming

COMP1021 Using For Loops with Turtle Graphics Page 2


For Loops in Turtle Graphics
• Let’s look at using for loops with graphics

• The basic difference between while loops


and for loops:
• While loops – sometimes you don’t know
how many times the loop will repeat
• For loops – you exactly control the start value,
end value and increment value, so you can work
out exactly how many times the loop will repeat

COMP1021 Using For Loops with Turtle Graphics Page 3


Drawing a Square Using a For Loop
• Let’s use a for loop to make a square:
for i in range(4):
turtle.forward(400)
turtle.right(90)

The content of the loop is


executed four times, to draw
four sides of the square

The letter ‘i’ is quite commonly used for the loop variable of a
loop (‘i’ for ‘index’), although you can use any variable name
Drawing a Star Shape Using a For Loop
• You can alter the program to draw a star shape
• This for loop runs five times to create the five
lines of the star:

for i in range(5):
turtle.forward(400)
turtle.right(144)

COMP1021 Using For Loops with Turtle Graphics Page 5


Drawing a Star Shape Using a For Loop
• You can alter the program to draw a star shape
• This for loop runs five times to create the five
lines of the star:
for _ in range(5):
turtle.forward(400)
turtle.right(144)
You can use an ‘_’ instead of a variable here
because the items (i.e. the numbers) are not
referred to anywhere inside the loop

COMP1021 Using For Loops with Turtle Graphics Page 6


Spiral Patterns Created Using Turtle
• In the following two examples patterns are created using
for loops with some cleverly chosen numbers
for i in range(0, 500, 5):
turtle.forward(i) Run 100 times,
Spiral
turtle.right(91) where
i = 0, 5, …, 495 Pattern 1
Turning by 91 degrees
creates a kind of spiral
pattern whereas
turning by 90 degrees
will produce this:
for i in range(0, 400, 2):
turtle.forward(i)
Spiral
Run 200 times,
turtle.right(89) where i = 0, 2, …, 398 Pattern 2
for i in range(401, 0, -2): Run 201 times
turtle.forward(i)
turtle.right(89)
The first loop makes this:

The second loop makes this:


Drawing a ‘Flower’ Using a Nested Loop
• In this example, a nested for loop (a for loop inside
another for loop) is used to draw a flower
• The inner loop draws a hexagon and the outer loop uses
the inner loop ten times to draw the flower:
Draw a single for _ in range(10):
hexagon using The outer
the inner loop for _ in range(6): loop draws
turtle.forward(120) hexagons
turtle.right(60) around one
full circle
turtle.right(36) (10 * 36 = 360)

COMP1021 Using For Loops with Turtle Graphics Page 10


The Flower Pattern Created By Hexagons
Drawing a Pyramid of Dots
• In this example, a nested loop draws a pyramid of
turtle dots using turtle.dot()
• The code is shown below:
size = 20
Create a single
for i in range(0, 15, 2):
row of dots in
for j in range(i + 1):
the inner loop,
turtle.dot(size)
e.g.:
turtle.forward(size)

turtle.backward(size * (i + 2))
Move the turtle to turtle.right(90)
the starting point turtle.forward(size)
of the next row turtle.left(90)
Drawing the Rows of Dots
for i in range(0, 15, 2):
for j in range( i + 1 ):
...
• As you can see from the loops, the inner loop runs a
number of times based on the value of the outer loop
– The first time the inner loop runs, it draws 1 dot
– The second time it runs, it draws 3 dots

– The last time it runs, it draws 15 dots

COMP1021 Using For Loops with Turtle Graphics Page 13


turtle.dot() and turtle.up()
• You have learned that the turtle does not draw
lines when you run turtle.up() before you
move the turtle
• However, turtle.dot() is not affected by
turtle.up() or turtle.down()
• In our example, import turtle
turtle.up() has
been used at the start turtle.color("brown")
of the program turtle.speed(0)
but the dots can still turtle.up()
turtle.hideturtle()
be drawn
...
A Pyramid of Dots

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy