0% found this document useful (0 votes)
7 views

Nested Loops

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)
7 views

Nested Loops

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/ 12

COMP1021

Introduction to Computer Science

An Example of a Nested Loop

David Rossiter and Gibson Lam


Outcomes
• After completing this presentation, you are
expected to be able to:
1. Use nested while loops to create a target
pattern

COMP1021 An Example of a Nested Loop Page 2


Using Nested
Loops
• On the right is a
flower image
created by a
single program
• The petals are a
good example of
using nested loops
The Program Stages
• Stage 1: Get the graphics started
– Import the turtle module, fast speed
• Stage 2: Create the curved stem
– Draw a small part of a circle
• Stage 3: Draw the petals
– Uses a nested loop
• Stage 4: Draw the flower centre
– Draw a yellow circle
Stage 1 – Get the Graphics Started
• Like many of the programs we have seen, the
first step is to import the turtle module and set
some initial parameters i.e.:

import turtle

turtle.speed(0)

COMP1021 An Example of a Nested Loop Page 5


Stage 2 – Create the Curved Stem
• We can create the stem of the flower
using the turtle.circle() command:
turtle.width(20)
turtle.color("green")

turtle.up() # Don't draw while we move


turtle.goto(100, -400) # Move the turtle to bottom right
turtle.left(90) # Point the turtle upwards
turtle.down() # Start drawing from now onwards
turtle.circle(1000, 30) # Draw part of a large circle

COMP1021 An Example of a Nested Loop Page 6


Stage 3 – Draw the Petals
while . . .condition . . . :
. . .statement(s) . . .
while . . .condition . . . :
. . .statement(s) . . .
• As you already know, a loop inside another loop
is called a nested loop
• It doesn’t matter what type of loop it is; any type of
loop inside any type of loop is called a nested loop
• So far we know about while loops, in another
presentation we will learn about for loops
Designing the Nested Loop Structure
• Let’s consider how we can use a nested loop
• Outer loop: repeat 8 times, for drawing 8 petals

• Move to the position of the first circle


• Inner loop: repeat 13 times, for drawing 13 circles
• Draw a circle of the appropriate size
• Move to the position of the next circle

• Go backwards, to the centre position of the flower


• Rotate the turtle by 45 degrees, ready for the next petal

• We will first show the inner loop, then the outer loop
A Petal circle_number= 12 diameter= 19.5
circle_number= 11 diameter= 36.0
circle_number= 10 diameter= 49.5
circle_number= 9 diameter= 60.0
circle_number= 8 diameter= 67.5
circle_number= 7 diameter= 72.0
circle_number= 6 diameter= 73.5
circle_number= 5 diameter= 72.0
circle_number= 4 diameter= 67.5
circle_number= 3 diameter= 60.0
Direction of circle_number= 2 diameter= 49.5
adding circles circle_number= 1 diameter= 36.0
• In this slide different circle_number= 0 diameter= 19.5
shades of grey are used • To make the leaf shape a clever formula
just to help you see the is used which uses the circle number to
different circles determine an appropriate diameter
The Inner Loop
gap_between_circle = 10 These 3 variables Direction
total_circles = 13 are used in the of adding
circles
following code
circle_number = 0
while circle_number < total_circles: Repeat 13 times

diameter = (circle_number + 1) * 1.5 Calculate the


* (total_circles - circle_number) diameter using a
clever formula, based
turtle.dot(diameter) on the circle number
turtle.forward(gap_between_circle) (you don’t need to
circle_number = circle_number + 1 understand the
maths)
Draw a circle and then move forward
(away from the center of the flower) to get in position for the next circle
starting_distance = 40
These 3 variables
total_petals = 8
are used in the
The turtle moves
following code
petal_number = 0 forward when it
while petal_number < total_petals: makes a petal; now
turtle.forward(starting_distance) go backwards to reach
the flower center once
The code shown in the again, ready for the
previous slide goes here creating the next petal
turtle.backward(starting_distance
+ (total_circles * gap_between_circle) )
If there’s 8 petals this
turtle.left(360/ total_petals) angle will be 360/8
= 45 degrees
petal_number = petal_number + 1

The Outer Loop


Stage 4 – Draw the Flower Centre
# Set the turtle drawing colour
turtle.color("yellow")

# Make a circle, using the drawing colour


turtle.dot(160)

# Sometimes we need this:


turtle.done()

You might also like