15. Exercises

  1. Write a program that prints We like Python's turtles! 1000 times.

    Now, update that program to prompt the user for an integer and then print the same message the given number of times.

  2. Write a program that prints out the lyrics to the song “99 Bottles of Beer on the Wall”

  3. Write a program that prints even integers from 0 to 50.

  4. Write a program that uses a for loop to print
    One of the months of the year is January
    One of the months of the year is February
    One of the months of the year is March
    etc ...
  5. Use for loops to make a turtle draw these regular polygons (regular means all sides the same lengths, all angles the same):

    • An equilateral triangle
    • A square
    • A hexagon (six sides)
    • An octagon (eight sides)
    # draw an equilateral triangle
    import turtle
    
    wn = turtle.Screen()
    norvig = turtle.Turtle()
    
    for i in range(3):
        norvig.forward(100)
    
        # the angle of each vertice of a regular polygon
        # is 360 divided by the number of sides
        norvig.left(360/3)
    
    wn.exitonclick()
    
    # draw a square
    import turtle
    
    wn = turtle.Screen()
    kurzweil = turtle.Turtle()
    
    for i in range(4):
        kurzweil.forward(100)
        kurzweil.left(360/4)
    
    wn.exitonclick()
    
    # draw a hexagon
    import turtle
    
    wn = turtle.Screen()
    dijkstra = turtle.Turtle()
    
    for i in range(6):
        dijkstra.forward(100)
        dijkstra.left(360/6)
    
    wn.exitonclick()
    
    # draw an octogon
    import turtle
    
    wn = turtle.Screen()
    knuth = turtle.Turtle()
    
    for i in range(8):
        knuth.forward(75)
        knuth.left(360/8)
    
    wn.exitonclick()
    
  6. Write a program that asks the user for the number of sides, the length of the side, the color, and the fill color of a regular polygon.

  7. A drunk pirate makes a random turn and then takes 100 steps forward, makes another random turn, takes another 100 steps, turns another random amount, etc. A social science student records the angle of each turn before the next 100 steps are taken. Her experimental data is 160, -43, 270, -97, -43, 200, -940, 17, -86. (Positive angles are counter-clockwise.) Use a turtle to draw the path taken by our drunk friend.

  8. On a piece of scratch paper, trace the the path of the turtle in the following program. When you are done, press run and check your answer.

  9. Write a program to draw a shape like this:

    ../_images/star1.png
  10. Write a program to draw a face of a clock that looks something like this:

    ../_images/tess_clock1.png
  11. Write a program to draw some kind of picture. Be creative and experiment with the turtle methods provided in Summary of Turtle Methods.

  12. Create a turtle and assign it to a variable. When you print its type, what do you get?

  13. A sprite is a simple spider shaped thing with n legs coming out from a center point. The angle between each leg is 360 / n degrees.

    Write a program to draw a sprite where the number of legs is provided by the user.

  14. Use a for statement to print 10 random numbers.

  15. Repeat the above exercise but this time print 10 random numbers between 25 and 35.

  16. The Pythagorean Theorem tells us that the length of the hypotenuse of a right triangle is related to the lengths of the other two sides. Look through the math module and see if you can find a function that will compute this relationship for you. Once you find it, write a short program to try it out.

  17. Search on the internet for a way to calculate an approximation for pi. There are many that use simple arithmetic. Write a program to compute the approximation and then print that value as well as the value of math.pi from the math module.

Graded Lesson Assignment

Assume you have a list of numbers nums = [12, 10, 32, 3, 66, 17, 42, 99, 20]

  1. Write a loop that prints each of the numbers on a new line, like this:

    12
    10
    ...etc
    
  2. Write a second loop that prints each number and its square on a new line, precisely like this:

    The square of 12 is 144
    The square of 10 is 100
    ...etc
    

Ready to submit your code? Time to fork, and submit a Pull Request to: this Github Repository.