6.11. Exercises

  1. Use the drawsquare function we wrote in this chapter in a program to draw the image shown below. Assume each side is 20 units. (Hint: notice that the turtle has already moved away from the ending point of the last square when the program ends.)

    ../_images/five_squares.png

    (ex_5_1)

    23
     
    1
    import turtle
    2
    3
    def drawSquare(t, sz):
    4
        """Make turtle t draw a square of with side sz."""
    5
        for i in range(4):
    6
            t.forward(sz)
    7
            t.left(90)
    8
    9
    wn = turtle.Screen()       # Set up the window and its attributes
    10
    wn.bgcolor("lightgreen")
    11
    12
    alex = turtle.Turtle()     # create alex
    13
    alex.color('hotpink')
    14
    alex.pensize(3)
    15
    16
    for i in range(5):
    17
        drawSquare(alex, 20)   # Call the function to draw the square
    18
        alex.penup()
    19
        alex.forward(40)       # move alex to the starting position for the next square
    20
        alex.pendown()
    21
    22
    wn.exitonclick()
    23

    (func_q1_answer)

    Show Comments
  1. Write a program to draw this. Assume the innermost square is 20 units per side, and each successive square is 20 units bigger, per side, than the one inside it.

    ../_images/nested_squares.png

    (ex_5_2)

  1. Write a non-fruitful function drawPoly(someturtle, somesides, somesize) which makes a turtle draw a regular polygon. When called with drawPoly(tess, 8, 50), it will draw a shape like this:

    ../_images/regularpolygon.png

    (ex_5_3)

    16
     
    1
    import turtle
    2
    3
    def drawPoly(t, num_sides, side_length):
    4
        for i in range(num_sides):
    5
            t.forward(side_length)
    6
            t.left(360/num_sides)
    7
    8
    wn = turtle.Screen()       # Set up the window and its attributes
    9
    wn.bgcolor("lightgreen")
    10
    11
    tess = turtle.Turtle()
    12
    tess.color('hotpink')
    13
    tess.pensize(3)
    14
    15
    drawPoly(tess, 8, 50)
    16

    (func_q3_answer)

    Show Comments
  1. Draw this pretty pattern.

    ../_images/tess08.png

    (ex_5_4)

  1. The two spirals in this picture differ only by the turn angle. Draw both.

    ../_images/tess_spirals.png

    (ex_5_5)

    36
     
    1
    import turtle
    2
    3
    def drawSpiral(t, angle):
    4
        ''' takes a turtle, t, and an angle in degrees '''
    5
        length = 1
    6
        for i in range(84):
    7
            t.forward(length)
    8
            t.right(angle)
    9
            length = length + 2
    10
    11
    12
    wn = turtle.Screen()       # Set up the window and its attributes
    13
    wn.bgcolor("lightgreen")
    14
    15
    guido = turtle.Turtle()    # create guido
    16
    guido.color('blue')
    17
    18
    ## draw the first spiral ##
    19
    # position guido
    20
    guido.penup()
    21
    guido.backward(110)
    22
    guido.pendown()
    23
    24
    # draw the spiral using a 90 degree turn angle
    25
    drawSpiral(guido, 90)
    26
    27
    28
    ## draw the second spiral ##
    29
    # position guido
    30
    guido.home()
    31
    guido.penup()
    32
    guido.forward(90)
    33
    guido.pendown()
    34
    35
    drawSpiral(guido, 89)
    36

    (func_q5_answer)

    Show Comments
  1. Write a non-fruitful function drawEquitriangle(someturtle, somesize) which calls drawPoly from the previous question to have its turtle draw a equilateral triangle.

    (ex_5_6)

  1. Write a fruitful function sumTo(n) that returns the sum of all integer numbers up to and including n. So sumTo(10) would be 1+2+3...+10 which would return the value 55. Use the equation (n * (n + 1)) / 2.

    (ex_5_7)

    14
     
    1
    from test import testEqual
    2
    3
    def sumTo(n):
    4
        result = (n * (n + 1)) / 2
    5
        return result
    6
    7
    # Now lets see how well this works
    8
    t = sumTo(0)
    9
    print("The sum from 1 to 0 is",t)
    10
    t = sumTo(10)
    11
    print("The sum from 1 to 10 is",t)
    12
    t = sumTo(5)
    13
    print("The sum from 1 to 5 is",t)
    14

    (func_q7_answer)

    Show Comments
  1. Write a function areaOfCircle(r) which returns the area of a circle of radius r. Make sure you use the math module in your solution.

    (ex_5_8)

  1. Write a non-fruitful function to draw a five pointed star, where the length of each side is 100 units.

    ../_images/star.png

    (ex_5_9)

    10
     
    1
    import turtle
    2
    3
    def drawFivePointStar(t):
    4
        for i in range(5):
    5
            t.forward(100)
    6
            t.left(216)
    7
    8
    wolfram = turtle.Turtle()
    9
    drawFivePointStar(wolfram)
    10

    (func_q9_answer)

    Show Comments
  1. Extend your program above. Draw five stars, but between each, pick up the pen, move forward by 350 units, turn right by 144, put the pen down, and draw the next star. You’ll get something like this (note that you will need to move to the left before drawing your first star in order to fit everything in the window):

    ../_images/five_stars.png

    What would it look like if you didn’t pick up the pen?

    (ex_5_10)

  1. Extend the star function to draw an n pointed star. (Hint: n must be an odd number greater or equal to 3).

    (ex_5_11)

    10
     
    1
    import turtle
    2
    3
    def drawStar(t, n):
    4
        for i in range(n):
    5
            t.forward(100)
    6
            t.left(180 - 180/n)
    7
    8
    stroustrup = turtle.Turtle()
    9
    drawStar(stroustrup, 7)
    10

    (func_q11_answer)

    Show Comments
  1. Write a function called drawSprite that will draw a sprite. The function will need parameters for the turtle, the number of legs, and the length of the legs. Invoke the function to create a sprite with 15 legs of length 120.

    (ex_5_12)

  1. Rewrite the function sumTo(n) that returns the sum of all integer numbers up to and including n. This time use the accumulator pattern.

    (ex_5_13)

    14
     
    1
    def sumTo(n):
    2
        sum = 0
    3
        for i in range(1,n+1):
    4
            sum = sum + i
    5
        return sum
    6
    7
    # Now lets see how well this works
    8
    t = sumTo(0)
    9
    print("The sum from 1 to 0 is",t)
    10
    t = sumTo(10)
    11
    print("The sum from 1 to 10 is",t)
    12
    t = sumTo(5)
    13
    print("The sum from 1 to 5 is",t)
    14

    (func_q13_answer)

    Show Comments
  1. Write a function called mySqrt that will approximate the square root of a number, call it n, by using Newton’s algorithm. Newton’s approach is an iterative guessing algorithm where the initial guess is n/2 and each subsequent guess is computed using the formula: newguess = (1/2) * (oldguess + (n/oldguess)).

    (ex_5_14)

  1. Write a function called myPi that will return an approximation of PI (3.14159…). Use the Leibniz approximation.

    (ex_5_15)

    17
     
    1
    def myPi(iters):
    2
        ''' Calculate an approximation of PI using the Leibniz
    3
        approximation with iters number of iterations '''
    4
        pi = 0
    5
        sign = 1
    6
        denominator = 1
    7
        for i in range(iters):
    8
            pi = pi + (sign/denominator)
    9
            sign = sign * -1  # alternate positive and negative
    10
            denominator = denominator + 2
    11
    12
        pi = pi * 4.0
    13
        return pi
    14
    15
    pi_approx = myPi(10000)
    16
    print(pi_approx)
    17

    (func_q15_answer)

    Show Comments
  1. Write a function called myPi that will return an approximation of PI (3.14159…). Use the Madhava approximation.

    (ex_5_16)

  1. Write a function called fancySquare that will draw a square with fancy corners (sprites on the corners). You should implement and use the drawSprite function from above. For an even more interesting look, how about adding small triangles to the ends of the sprite legs.

    (ex_5_17)

    23
     
    1
    import turtle
    2
    3
    def drawSprite(t, numlegs, leglength):
    4
       angle = 360/numlegs
    5
       for i in range(numlegs):
    6
          t.forward(leglength)
    7
          t.backward(leglength)
    8
          t.left(angle)
    9
    10
    def drawFancySquare(t, sz, lgs, lgl):
    11
       for i in range(4):
    12
           t.forward(sz)
    13
           drawSprite(t, lgs, lgl)
    14
           t.left(90)
    15
    16
    wn = turtle.Screen()
    17
    wn.bgcolor("lightgreen")
    18
    19
    alex = turtle.Turtle()
    20
    drawFancySquare(alex, 100, 10, 15)
    21
    22
    wn.exitonclick()
    23

    (func_q17_answer)

    Show Comments
  1. There was a whole program in A Turtle Bar Chart to create a bar chart with specific data. Creating a bar chart is a useful idea in general. Write a non-fruitful function called barChart, that takes the numeric list of data as a parameter, and draws the bar chart. Write a full program calling this function. The current version of the drawBar function unfortuately draws the top of the bar through the bottom of the label. A nice elaboration is to make the label appear completely above the top line. To keep the spacing consistent you might pass an extra parameter to drawBar for the distance to move up. For the barChart function make that parameter be some small fraction of maxheight+border. The fill action makes this modification particularly tricky: You will want to move past the top of the bar and write before or after drawing and filling the bar.

    (ex_5_18)

Next Section - 7. Selection