# 5.10. Exercises¶

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

1. Turtle objects have methods and attributes. For example, a turtle has a position and when you move the turtle forward, the position changes. Think about the other methods shown in the Summary of Turtle Methods page. Which attibutes, if any, does each method relate to? Does the method change the attribute?

1. 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()

1. Write a program to draw a shape like this: 1. Write a program to draw a face of a clock that looks something like this: 1. Write a program to draw some kind of picture. Be creative and experiment with the turtle methods.

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

## 5.10.1. Contributed Exercises¶

Write a program that prints I love Python! 15 times.
Turtle exercise:Ask the user for a side length and then draw a triangle with a turtle.
Turtle exercise: ask the user for a radius and then draw a circle with that radius with a turtle.
Turtle exercise: ask the user for a side length and then draw an octagon (8-sided) with a turtle.
Consider the program below which prints I love Python 10 times. To avoid hard-coding, change this program so that it asks a user for an input that specifies how many times I love Python should be printed. So if the user says 3 times, the program will print it 3 times. It should work for any input without you manually changing the code.

System Message: ERROR/3 (/home/bmiller/Runestone/web2py/applications/runestone/build/preview/_sources/index.rst, line 2)

Error in “mchoice” directive: invalid option value: (option: “multiple_answers”; value: ‘true’) no argument is allowed; “true” supplied.

.. mchoice:: question1

:correct: a, b
:feedback_a: você acertou
:feedback_b: você acertou
:feedback_c: você errou
:feedback_d: você errou

Qual a resposta correta?


Write a function to compute pi to 5000 places

Q-1: After completing the reading, what concepts are still unclear to you? If nothing is unclear, what did you find most interesting?

Write a turtle program that draws the letters LLL in green. In order to get credit you must use one loop repeating the same thing three times but at a different location, one for each L.

Use split (by the empty space) and join (by the “-“) to transform the string “Once upon a time” into “Once-upon-a-time”. Print your result. Write it all in one line.

Write a turtle program that asks the user “how many sides?”. If the number of sides is even, draw two regular polygon with that many sides, one inside the other. If the number of sides is odd, draw two regular polygons with that many sides, side by side.

Use the turtle module to draw an L. The side of the L should be 100 long and the width of the L should be 75 long. The top left should start at $$x=-100$$, $$y=100$$.

Turtles and Random: Write a turtle program that generates a random number of sides for a polygon between 3 and 8 (from triangle to octagon). If the number of sides is even, draw two regular polygons with that many sides, side by side, colored blue. If the number of sides is odd, draw two regular polygons with that many sides, side by side, one colored blue and the other red. Extra Credit: employ functions in your program.

Turtles and Random: Write a turtle program that generates a random number of sides for a polygon between 3 and 8 (from triangle to octagon). If the number of sides is even, draw two regular polygons with that many sides, side by side, colored blue. If the number of sides is odd, draw two regular polygons with that many sides, side by side, one colored blue and the other red. Extra Credit: employ functions in your program.

Use the turtle module to create a turtle tuck. Use tuck to draw a box with it’s center at ($$x=0$$, $$y=0$$). The width of the box should be 100 and the height of the box should be 200.