Chapter 10 Exercises¶
Fix 4 syntax errors in the code below to correctly draw a square
1from turtle import # use the turtle library
2space = screen() # create a turtle space
3alisha = Turtle # create a turtle named alisha
4alisha.setheading(90) # point due north
5for sides in [1, 2, 3]: # repeat the indented lines 4 times
6alisha.forward(100) # move forward by 100 units
7alisha.right(90) # turn by 90 degrees
8
(ch10ex1q)
The code currently draws a square. Change it so that it draws a triangle.
81from turtle import * # use the turtle library
2space = Screen() # create a turtle space
3alisha = Turtle() # create a turtle named alisha
4alisha.setheading(90) # point due north
5for sides in [1, 2, 3, 4]: # repeat the indented lines 4 times
6alisha.forward(100)
7alisha.right(90)
8
(ch10ex2q)
Fix the code below to draw a rectangle. You will need to fix the indention on 3 lines.
111from turtle import *
2space = Screen()
3carlos = Turtle()
4
5# repeat 2 times
6for i in [1,2]:
7carlos.forward(175)
8carlos.right(90)
9carlos.forward(150)
10carlos.right(90)
11
(ch10ex3q)
Fix the errors in the code so that it draws an octagon.
81from turtle import *
2space = Screen()
3liz = Turtle()
4liz.setheading(90)
5for sides in range(9)
6liz.forward(45)
7liz.right(50)
8
(ch10ex4q)
Fill in values for
x
on line 5 andy
on line 7 to allow the code below to correctly draw a pentagon.81from turtle import * # use the turtle library
2space = Screen() # create a turtle space
3will = Turtle() # create a turtle named will
4will.setheading(90) # point due north
5for sides in range(x): # repeat the indented lines
6will.forward(100) # move forward by 100 units
7will.right(y)
8
(ch10ex5q)
Complete the code on lines 5 and 7 to draw a hexagon.
81from turtle import *
2space = Screen()
3mia = Turtle()
4mia.setheading(90)
5for sides in
6mia.forward(40)
7mia.
8
(ch10ex6q)
Finish the code on lines 1, 2, 3, 6 and 8 below to correctly draw a triangle.
91from
2space =
3marie =
4
5# repeat
6for i in range():
7marie.forward(100)
8marie.left()
9
(ch10ex7q)
Finish the code to draw a 15 sided figure with each side having a length of 40.
41from turtle import *
2space = Screen()
3hi = Turtle()
4
(ch10ex8q)
Fix the indention in the code below to correctly draw 20 pentagons.
161from turtle import * # use the turtle library
2from sys import * # use the system library
3setExecutionLimit(50000) # let this take up to 50 seconds
4space = Screen() # create a turtle space
5zoe = Turtle() # create a turtle named zoe
6zoe.setheading(90) # point due north
7
8for repeats in range(20): # draw the pattern 20 times
9zoe.forward(10) # Offset the shapes a bit
10zoe.right(18) # And turn each one a bit
11
12# This part makes a pentagon
13for sides in range(5): # repeat 5 times
14zoe.forward(50) # move forward by 50 unit
15zoe.right(72) # turn by 72 degrees
16
(ch10ex9q)
The procedure below draws a square. Write code that uses the procedure to draw two squares connected by a line 50 units in length.
51def square(aTurtle):
2for sides in range(4):
3aTurtle.forward(100)
4aTurtle.right(90)
5
(ch10ex10q)
Fix the following code below to draw a circle of turtles using the
stamp
procedure. You will need to change 3 lines.111from turtle import *
2space = Screen()
3jose = Turtle()
4jose.shape("turtle")
5jose.
6for size in range():
7jose.forward(50)
8jose.stamp()
9jose.forward()
10jose.right(36)
11
(ch10ex11q)
Complete the code where the
x's
are so that the code draws 20 triangles.151from turtle import *
2from sys import * # use the system library
3setExecutionLimit(50000) # let this take up to 50 seconds
4space = Screen()
5t = x
6t.setheading(90)
7for repeats in range(x):
8t.color("blue")
9t.forward(10)
10t.left(18)
11for sides in range(x):
12t.color("green")
13t.forward(x)
14t.right(x)
15
(ch10ex12q)
Rewrite the following code to create a procedure to draw a square with a turtle. Pass the turtle and the size of the square as input (parameters) to the procedure.
81from turtle import * # use the turtle library
2space = Screen() # create a turtle space
3alisha = Turtle() # create a turtle named alisha
4alisha.setheading(90) # point due north
5for sides in [1, 2, 3, 4]: # repeat the indented lines 4 times
6alisha.forward(100) # move forward by 100 units
7alisha.right(90) # turn by 90 degrees
8
(ch10ex13q)
Currently, the code has a turtle drawing a straight line. Add 2 lines of code (1 before the loop and 1 in the loop) to make the turtle stamp in the line.
111from turtle import *
2space = Screen()
3tess = Turtle()
4tess.color("blue")
5tess.shape("turtle")
6
7
8for size in range(5, 60, 2):
9
10tess.forward(size)
11
(ch10ex14q)
Rewrite the following code to create a procedure to draw a rectangle with a turtle. Pass the turtle and the length and width of the rectangle as parameters to the procedure.
111from turtle import *
2space = Screen()
3carlos = Turtle()
4
5# repeat 2 times
6for i in [1, 2]:
7carlos.forward(175)
8carlos.right(90)
9carlos.forward(150)
10carlos.right(90)
11
(ch10ex15q)
Complete the code so that the turtle stamps a square pattern 20 times (it should look like a circle enclosing a couple of circles if you use a turn angle of 18)
61from turtle import *
2from sys import * # use the system library
3setExecutionLimit(50000) # let this take up to 50 seconds
4space = Screen()
5zoe = Turtle()
6
(ch10ex16q)
Create a procedure to draw 4 turtles at the 4 corners of a square using the
stamp
procedure.31
2
3
(ch10ex17q)
Create a procedure that takes in a turtle and integer parameter. The procedure should stamp a turtle shape into a circle in 20 steps with the forward number being equal to the parameter.
31
2
3
(ch10ex18q)
Write a procedure that takes a turtle and a number of sides as parameters and draws a polygon with that number of sides.
31
2
3
(ch10ex19q)
Write a procedure that takes a turtle, an int for the number of sides for a polygon, and an int for the number of times to draw that polygon. The procedure should draw that polygon that number of times in a circular path.
31
2
3
(ch10ex20q)