Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
17.8. 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 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,0000 milliseconds is 50 seconds.
Run the code to see what it draws.
By setting the pen color differently, we can distinguish the part that draws the shape, from the part that draws between the shapes.
Run the code to see what it draws.
You can use the coloring in the picture below to help you figure out the correct order of the lines below.
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.
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?
You have attempted
of
activities on this page