# Solutions 2.2: Turtle Programming

Let’s go over the solutions to the exercises from Lesson 2.2: Turtle Programming.

Below is the final code for each exercise. Note that we did not calculate the initial position or anything like that. We just tried several different values and kept the ones that looked the best for each exercise. Your values might be slightly different.

### Exercise 1

Write a program *two_squares.py* that uses the Turtle module to draw two squares of size 100. Draw them in such a way that they are next to each other 100 steps apart. Make sure that the drawing does not contain any other lines. As the last development step, make sure that the drawing is in the center of the *Python Turtle Graphics* window.

#### Solution

Since the squares are 100 steps high and 100 steps apart, the lower left corner of the right square has to be at point *(50,-50)*. Remember to use penup() and pendown() commands to move without drawing a line. We return *home* at the very end, so that it’s clear where the center of the canvas is.

```
from turtle import *
shape('turtle')
fillcolor('green')
penup()
setposition(50,-50)
pendown()
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
penup()
backward(200)
pendown()
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
penup()
home()
```

### Exercise 2

Write a program *triangle_75.py* that uses the Turtle module to draw a triangle that has one side of length 200, the other side of length 100, and the angle between these two sides is 75 degrees. Make sure that the drawing is (approximately) in the center of the *Python Turtle Graphics* window.

#### Solution

Since the horizontal side is 200 steps long, we have to move 100 steps to the left to have the triangle centered horizontally. The other side is almost vertical and 100 steps long, so to center the image vertically, we move 40 steps down. 45 or even 50 steps would work as well.

```
from turtle import *
shape('turtle')
fillcolor('green')
penup()
setposition(-100,-40)
pendown()
forward(200)
left(105)
forward(100)
setposition(-100,-40)
penup()
home()
```

### Exercise 3

Write a program *star.py* that draws a five-point star. Make sure that the star is in the center of your canvas.

#### Solution

Since the horizontal edge is 200 steps long, we need to start at -100 to have the star centered horizontally. By experimenting, we came up with a vertical starting point of about 35. Remember that the inner angle for a 5-point star is 36°, so the outer angle must be 144°.

```
from turtle import *
shape('turtle')
fillcolor('green')
penup()
setposition(-100,35)
pendown()
forward(200)
right(144)
forward(200)
right(144)
forward(200)
right(144)
forward(200)
right(144)
forward(200)
right(144)
penup()
home()
```