Making Patterns within Patterns¶
We now know the pattern for creating any polygon. We can wrap that pattern in another loop to create spirograph like patterns. The example below uses pentagons, but you can use other polygons instead.
Note
The outer for
loop in the code below on line 8 executes 20 times and the
inner for
loop on line 13 executes 5 times for each of the outer loop
values, 5 times when the outer loop value is 0, 5 times when the outer loop
value is 1, 5 times when the outer loop value is 2, and so on. The inner
loop is executed a total of 20 * 5 = 100 times. The turtle can take a long
time to finish drawing this pattern. Normally code in the browser is
limited to only running in 10 seconds or less. But, we can use the
Skulpt sys
library (short for system)
setExecutionLimit(milliseconds)
procedure which will let the code run
for up to the specified number of milliseconds. One second is 1,000
milliseconds, so 50,000 milliseconds is 50 seconds. Note also that this
only applies to the browser version of Python we are using in our
on-line book. You can’t and won’t need to do this running CPython on your computer.
By setting the pen color differently, we can distinguish the part that draws the shape, from the part that draws between the shapes.
You can use the coloring in the picture below to help you figure out the correct order of the lines below.
csp-10-3-2: csp-10-3-1: There is a way of arranging the statements below such that this image is created. The turtle will draw many triangles. Move the needed pieces of the program from the left into the space on the right. Indent lines as needed.from turtle import *
from sys import *
setExecutionLimit(50000)
---
wn = Screen()
mateo = Turtle()
mateo.setheading(90)
---
for repeats in range(20):
---
for repeats in range(20) #distractor
---
mateo.color("red")
mateo.forward(10)
mateo.left(18)
---
mateo.color("red")
mateo.forward(10)
mateo.left(12) #distractor
---
for sides in range(3):
---
for sides in range(4): #distractor
---
mateo.color("blue")
mateo.forward(50)
mateo.right(120)
You can use the coloring in the picture below to help you figure out the correct order of the lines below.
csp-10-3-4: csp-10-3-3: There is a way of arranging the statements below such that this image is created. The turtle will draw many triangles. Move the needed pieces of the program from the left into the space on the right. Indent lines as needed.from turtle import *
from sys import *
setExecutionLimit(50000)
---
wn = Screen()
mateo = Turtle()
mateo.setheading(90)
---
for repeats in range(20):
---
for repeats in range(20) #distractor
---
mateo.color("blue")
mateo.forward(10)
mateo.left(18)
---
for sides in range(3):
---
for sides in range(3) #distractor
---
mateo.color("red")
mateo.forward(50)
mateo.right(120)
---
mateo.color("red")
mateo.forward(50)
mateo.right(60) #distractor
The following example has 4 errors. Can you fix the errors so that the code compiles and runs?
The following example has 4 errors. Can you fix the errors so that the code compiles and runs?