Chapter 10 Exercises

  1. Fix 4 syntax errors in the code below to correctly draw a square

     
    1
    from turtle import       # use the turtle library
    2
    space = screen()         # create a turtle space
    3
    alisha = Turtle          # create a turtle named alisha
    4
    alisha.setheading(90)    # point due north
    5
    for sides in [1, 2, 3]:  # repeat the indented lines 4 times
    6
        alisha.forward(100)  # move forward by 100 units
    7
        alisha.right(90)     # turn by 90 degrees
    8

    (ch10ex1q)

  2. The code currently draws a square. Change it so that it draws a triangle.

    8
     
    1
    from turtle import *        # use the turtle library
    2
    space = Screen()            # create a turtle space
    3
    alisha = Turtle()           # create a turtle named alisha
    4
    alisha.setheading(90)       # point due north
    5
    for sides in [1, 2, 3, 4]:  # repeat the indented lines 4 times
    6
        alisha.forward(100)
    7
        alisha.right(90)
    8

    (ch10ex2q)

  3. Fix the code below to draw a rectangle. You will need to fix the indention on 3 lines.

    11
     
    1
    from turtle import *
    2
        space = Screen()
    3
    carlos = Turtle()
    4
    5
    # repeat 2 times
    6
    for i in [1,2]:
    7
        carlos.forward(175)
    8
        carlos.right(90)
    9
    carlos.forward(150)
    10
    carlos.right(90)
    11

    (ch10ex3q)

  4. Fix the errors in the code so that it draws an octagon.

    8
     
    1
    from turtle import *
    2
    space = Screen()
    3
    liz = Turtle()
    4
    liz.setheading(90)
    5
    for sides in range(9)
    6
        liz.forward(45)
    7
    liz.right(50)
    8

    (ch10ex4q)

  5. Fill in values for x on line 5 and y on line 7 to allow the code below to correctly draw a pentagon.

    8
     
    1
    from turtle import *    # use the turtle library
    2
    space = Screen()        # create a turtle space
    3
    will = Turtle()         # create a turtle named will
    4
    will.setheading(90)     # point due north
    5
    for sides in range(x):  # repeat the indented lines
    6
        will.forward(100)   # move forward by 100 units
    7
        will.right(y)
    8

    (ch10ex5q)

  6. Complete the code on lines 5 and 7 to draw a hexagon.

    8
     
    1
    from turtle import *
    2
    space = Screen()
    3
    mia = Turtle()
    4
    mia.setheading(90)
    5
    for sides in
    6
        mia.forward(40)
    7
        mia.
    8

    (ch10ex6q)

  7. Finish the code on lines 1, 2, 3, 6 and 8 below to correctly draw a triangle.

    9
     
    1
    from
    2
    space =
    3
    marie =
    4
    5
    # repeat
    6
    for i in range():
    7
        marie.forward(100)
    8
        marie.left()
    9

    (ch10ex7q)

  8. Finish the code to draw a 15 sided figure with each side having a length of 40.

    4
     
    1
    from turtle import *
    2
    space = Screen()
    3
    hi = Turtle()
    4

    (ch10ex8q)

  9. Fix the indention in the code below to correctly draw 20 pentagons.

    16
     
    1
    from turtle import *      # use the turtle library
    2
    from sys import *         # use the system library
    3
    setExecutionLimit(50000)  # let this take up to 50 seconds
    4
    space = Screen()          # create a turtle space
    5
    zoe = Turtle()            # create a turtle named zoe
    6
    zoe.setheading(90)        # point due north
    7
    8
    for repeats in range(20):  # draw the pattern 20 times
    9
        zoe.forward(10)        # Offset the shapes a bit
    10
        zoe.right(18)          # And turn each one a bit
    11
    12
    # This part makes a pentagon
    13
    for sides in range(5):     # repeat 5 times
    14
        zoe.forward(50)        # move forward by 50 unit
    15
        zoe.right(72)          # turn by 72 degrees
    16

    (ch10ex9q)

  10. The procedure below draws a square. Write code that uses the procedure to draw two squares connected by a line 50 units in length.

    5
     
    1
    def square(aTurtle):
    2
        for sides in range(4):
    3
            aTurtle.forward(100)
    4
            aTurtle.right(90)
    5

    (ch10ex10q)

  11. Fix the following code below to draw a circle of turtles using the stamp procedure. You will need to change 3 lines.

    11
     
    1
    from turtle import *
    2
    space = Screen()
    3
    jose = Turtle()
    4
    jose.shape("turtle")
    5
    jose.
    6
    for size in range():
    7
        jose.forward(50)
    8
        jose.stamp()
    9
        jose.forward()
    10
        jose.right(36)
    11

    (ch10ex11q)

  12. Complete the code where the x's are so that the code draws 20 triangles.

    15
     
    1
    from turtle import *
    2
    from sys import *         # use the system library
    3
    setExecutionLimit(50000)  # let this take up to 50 seconds
    4
    space = Screen()
    5
    t = x
    6
    t.setheading(90)
    7
    for repeats in range(x):
    8
        t.color("blue")
    9
        t.forward(10)
    10
        t.left(18)
    11
        for sides in range(x):
    12
            t.color("green")
    13
            t.forward(x)
    14
            t.right(x)
    15

    (ch10ex12q)

  13. 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.

    8
     
    1
    from turtle import *        # use the turtle library
    2
    space = Screen()            # create a turtle space
    3
    alisha = Turtle()           # create a turtle named alisha
    4
    alisha.setheading(90)       # point due north
    5
    for sides in [1, 2, 3, 4]:  # repeat the indented lines 4 times
    6
        alisha.forward(100)     # move forward by 100 units
    7
        alisha.right(90)        # turn by 90 degrees
    8

    (ch10ex13q)

  14. 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.

    11
     
    1
    from turtle import *
    2
    space = Screen()
    3
    tess = Turtle()
    4
    tess.color("blue")
    5
    tess.shape("turtle")
    6
    7
    8
    for size in range(5, 60, 2):
    9
    10
        tess.forward(size)
    11

    (ch10ex14q)

  15. 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.

    11
     
    1
    from turtle import *
    2
    space = Screen()
    3
    carlos = Turtle()
    4
    5
    # repeat 2 times
    6
    for i in [1, 2]:
    7
        carlos.forward(175)
    8
        carlos.right(90)
    9
        carlos.forward(150)
    10
        carlos.right(90)
    11

    (ch10ex15q)

  16. 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)

    6
     
    1
    from turtle import *
    2
    from sys import *         # use the system library
    3
    setExecutionLimit(50000)  # let this take up to 50 seconds
    4
    space = Screen()
    5
    zoe = Turtle()
    6

    (ch10ex16q)

  17. Create a procedure to draw 4 turtles at the 4 corners of a square using the stamp procedure.

    3
     
    1
    2
    3

    (ch10ex17q)

  18. 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.

    3
     
    1
    2
    3

    (ch10ex18q)

  19. Write a procedure that takes a turtle and a number of sides as parameters and draws a polygon with that number of sides.

    3
     
    1
    2
    3

    (ch10ex19q)

  20. 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.

    3
     
    1
    2
    3

    (ch10ex20q)

Next Section - Chapter 11 - Repeating Steps with Images