Chapter 10 Exercises¶
Fix 4 syntax errors in the code below to correctly draw a square
Add a
*
at the end of line 1. Changescreen
toScreen
in line 2. Add()
at the end of line 3. Change line 5 to[1,2,3,4]
.The code currently draws a square. Change it so that it draws a triangle.
Change the list to only have 3 items in it and change the angle to be 120.
Fix the code below to draw a rectangle. You will need to fix the indention on 3 lines.
Change the indention on lines 2, 9 and 10 as shown below.
Fix the errors in the code so that it draws an octagon.
It should be range(8) and add a colon afterwards. Fix the indentation on the last line to be in the for loop, and switch the two numbers in the
forward
andright
methods.Fill in values for
x
on line 5 andy
on line 7 to allow the code below to correctly draw a pentagon.Change
x
to5
andy
to72
.Complete the code on lines 5 and 7 to draw a hexagon.
Line 5 needs to say “for sides in range(6)” and line 7 has to have the turtle turning right by 60.
Finish the code on lines 1, 2, 3, 6 and 8 below to correctly draw a triangle.
Add
turtle import *
to the end of line 1. AddScreen()
to the end of line 2. AddTurtle()
at the end of line 3. Set line 6 to repeat 3 times. Set line 8 to turn left 120 degrees.Finish the code to draw a 15 sided figure with each side having a length of 40.
You need a for loop that iterates through 15 elements. In the body of the loop, the turtle must go forward 40 and turn left 24.
Fix the indention in the code below to correctly draw 20 pentagons.
Indent lines 13-15 as shown below.
The procedure below draws a square. Write code that uses the procedure to draw two squares connected by a line 50 units in length.
Call the statements needed to create a turtle. Then call the function, turn it left, move it forward, and call the function again
Fix the following code below to draw a circle of turtles using the
stamp
procedure. You will need to change 3 lines.On line 5 add
penup()
. On line 6 change it to(10)
. On line 9 change it to(-50)
.Complete the code where the
x's
are so that the code draws 20 triangles.You need to call the Turtle() and then set the range to 20, then the next range to 3 and make the turtle go foward 60 and right 120.
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.
Define the procedure as shown below. Create a turtle and do all set-up before calling the procedure.
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.
You have to add a penup statement before the loop and a stamp statement in the loop.
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.
Define the procedure to draw a rectangle given a turtle, the length, and the width. Call the procedure to test it.
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)
Make sure to use penup before the loop. Inside the loop, there should be another loop, and you should call the stamp method inside both loops.
Create a procedure to draw 4 turtles at the 4 corners of a square using the
stamp
procedure.Define the procedure as shown below.
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.
In the procedure, have the two parameters. Change the turtle shape to “turtle” and use penup. Then have a loop for range of 20. Inside the loop make the turtle go forward by the int parameter value. Use the stamp method and then the turn right by 18.
Write a procedure that takes a turtle and a number of sides as parameters and draws a polygon with that number of sides.
Create a procedure as shown below. Call it to test it.
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.