0% found this document useful (0 votes)
15 views6 pages

Python For Kids (Level1-Level 2) 3rd - Week

Uploaded by

tr.ryan.ict
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)
15 views6 pages

Python For Kids (Level1-Level 2) 3rd - Week

Uploaded by

tr.ryan.ict
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/ 6

Python for kids | Sue’s Academy

Chapter 4
Loops Are Fun
We’ve used loops since our very first program to draw repeating shapes. Now it’s time to learn
how to build our own loops from scratch. Whenever we need to do something over and over
again in a program, loops allow us to repeat those steps without having to type each one
separately. Figure 4-1 shows a visual example — a rosette made up of four circles.

Figure 4-1. A four-circle rosette pattern

Copyright @ Sue’s academy 29


Python for kids | Sue’s Academy

Rosette.py
import turtle
t = turtle.Pen()
t.circle(100)
# This makes our first circle (pointing north)
t.left(90) # Then the turtle turns left 90 degrees
t.circle(100) # This makes our second circle (pointing west)
t.left(90) # Then the turtle turns left 90 degrees
t.circle(100) # This makes our third circle (pointing south)
t.left(90) # Then the turtle turns left 90 degrees
t.circle(100) # This makes our fourth circle (pointing east)

This code works, but doesn’t it feel repetitive? We typed the code to draw a circle four times
and the code to turn left three times.

Building Your Own for Loops

To build our own loop, we first need to identify the repeated steps. The instructions that we’re
repeating in the preceding code are t.circle(100) to draw a turtle circle with a radius of 100 pixels and
t.left(90) to turn the turtle left 90 degrees before drawing the next circle.
Second, we need to figure out how many times to repeat those steps. We want four circles, so let’s
start with four.
Now that we know the two repeated instructions and the number of times to draw the circle, it’s
time to build our for loop.
A for loop in Python iterates over a list of items, or repeats once for each item in a list — like the
numbers 1 through 100, or 0 through 9. We want our loop to run four times — once for each circle — so
we need to set up a list of four numbers.

Using a for Loop to Make a Rosette with Four Circles

For x in range(4): #(range(4)) gives you a list of four numbers starting at 0


t.circle(100)
t.left(90)

Copyright @ Sue’s academy 30


Python for kids | Sue’s Academy

Modifying Our for Loop to Make a Rosette with Six Circles

we get six circles around the center of the screen this time, as shown in Figure 4-2

Figure 4-2. A rosette of six circles

Copyright @ Sue’s academy 31


Python for kids | Sue’s Academy

Improving Our Rosette Program with User Input

RosetteGoneWild.py
import turtle
t = turtle.Pen() # Ask the user for the number of circles in their rosette, default to 6
number_of_circles = int(turtle.numinput("Number of circles", "How many circles in your rosette?", 6))
for x in range(number_of_circles):
t.circle(100)
t.left(360/number_of_circles)

Copyright @ Sue’s academy 32


Python for kids | Sue’s Academy

For example, if the user enters 30 as the number of circles, 360 ÷ 30 would give us a 12-degree turn
between each of the 30 circles around our center point, as shown in Figure 4-3

Figure 4-3. A user-defined rosette of 30 circles

Copyright @ Sue’s academy 33


Python for kids | Sue’s Academy

Figure 4-4. A little imagination and a touch of code can turn our rosette program into a lot of colorful fun!

Copyright @ Sue’s academy 34

You might also like