# Turtle Graphics with loops

## Shapes

To start, let's try some basic designs without loops. &#x20;

### Square

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Lab8p6pAfbQR9ER33nu%2F-LabBdbNmdjk0_vvqgdB%2FScreen%20Shot%202019-03-22%20at%204.58.00%20PM.png?alt=media\&token=3b9151da-0202-4ad8-8a96-0b5eadb925d9)

{% code title="square.py" %}

```python
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
```

{% endcode %}

{% hint style="info" %}
To add color to your design, wrap the following lines of code before and after the turtle movements.
{% endhint %}

```python
turtle.fillcolor("green")
turtle.begin_fill()
turtle.end_fill()
```

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Ladzl-zS5e9V9x5qaev%2F-Lae5cIM0RrtA9znIEga%2Fgreenturtle.PNG?alt=media\&token=de439bad-f917-4af7-bc8a-42efe92c7273)

```python
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.fillcolor("green")
turtle.begin_fill()
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.end_fill()
```

{% hint style="info" %}
turtle.pencolor("red") # this statement changes the pen's color.
{% endhint %}

### Rectangle

Let's draw a rectangle using variables.  In Python, you name a variable and assign it a value.   Replace each length and angle with a variable.

{% code title="rectangle.py" %}

```python
import turtle
turtle.showturtle()
turtle.shape("turtle")
length1 = 100
length2 = 200
angle = 90
turtle.forward(length1)
turtle.right(angle)
turtle.forward(length2)
turtle.right(angle)
turtle.forward(length1)
turtle.right(angle)
turtle.forward(length2)
turtle.right(angle)

```

{% endcode %}

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Ladzl-zS5e9V9x5qaev%2F-Lae8AfTjswlnkvkqy3H%2Frectangle.PNG?alt=media\&token=b59b85b5-03c4-4904-a1e3-473bd188b15f)

### Zigzag

### Star

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Lab8p6pAfbQR9ER33nu%2F-LabCzFOqecNZOUehZ5d%2FScreen%20Shot%202019-03-22%20at%205.04.03%20PM.png?alt=media\&token=c65532e2-57e3-45a1-94b6-17e0a201c6cd)

{% code title="zigzag.py" %}

```python
import turtle
length = 25
angle = 45
turtle.showturtle()
turtle.shape("turtle")
turtle.forward(length)
turtle.right(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.right(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.right(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.right(angle)
turtle.forward(length)
```

{% endcode %}

### Spiral

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-LabF1G0CGncExeIlosu%2F-LabFDl4ly9naFIKqq14%2FScreen%20Shot%202019-03-22%20at%205.13.03%20PM.png?alt=media\&token=d627a530-2165-4d8f-a3b0-33f61c01ef73)

{% code title="spiral.py" %}

```python
import turtle
length = 10
angle = 90
turtle.showturtle()
turtle.shape("turtle")
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
```

{% endcode %}

## Loops

Loops are used when you have a block of code that you want to repeat.  &#x20;

A for loop is used when you have a block of code which you want to repeat a fixed number of times.   The for loop iterates through the block of indented code.

|                   |                                           |
| ----------------- | ----------------------------------------- |
| for x in range(): | x is a variable that steps through values |
| \<do something>   | do something in the indented block        |

{% code title="for.py" %}

```python
for x in range(4):
    print(x)
```

{% endcode %}

```bash
$ python for.py
0
1
2
3
4
```

### Square

{% code title="square2.py" %}

```python
import turtle
turtle.showturtle()
turtle.shape("turtle")
for x in range(4):
    turtle.forward(50)
    turtle.right(90)
```

{% endcode %}

### Spiral

{% code title="spiral2.py" %}

```python
import turtle
length = 10
angle = 90
turtle.showturtle()
turtle.shape("turtle")
for x in range(10):
    turtle.forward(length+length)
    turtle.right(angle)
    length = length + 10
```

{% endcode %}

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-LaeFHjsKBfFuwqlPQ1X%2F-LaeFyQY-Nx7yWcgnGLV%2Fspiral3.PNG?alt=media\&token=09b8d2e1-cad1-4758-ba0a-c1f53f82add9)

{% code title="spiral3.py" %}

```python
import turtle
angle = 91
turtle.showturtle()
turtle.shape("turtle")
for x in range(100):
    turtle.forward(x)
    turtle.left(angle)
```

{% endcode %}

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-LaeGwIpWVy5hXz0eRy2%2F-LaeNNHjJyf9U_1MwXpn%2Fspiral4.PNG?alt=media\&token=a95ace95-f3f6-41e0-92cb-fc88baa6b6d4)

{% code title="circle.py" %}

```python
import turtle
angle = 91
turtle.showturtle()
turtle.shape("turtle")
for x in range(100):
    turtle.circle(x)
    turtle.left(angle)
```

{% endcode %}

### Polygons

| Shape         | Sides | Interior Angle |
| ------------- | ----- | -------------- |
| Triangle      | 3     | 60             |
| Quadrilateral | 4     | 90             |
| Pentagon      | 5     | 108            |
| Hexagon       | 6     | 120            |
| Heptagon      | 7     | 128.571        |
| Octagon       | 8     | 135            |
| Nonagon       | 9     | 140            |
| Decagon       | 10    | 144            |
| Hendecagon    | 11    | 147.273        |
| Dodecagon     | 12    | 150            |

{% code title="triangle.py" %}

```python
import turtle
length = 100
angle = 120
turtle.showturtle()
turtle.shape("turtle")
for i in range(3):
    turtle.forward(length)
    turtle.right(angle)
```

{% endcode %}

### Star

Using a loop, we can produce an eight point star.

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-LbD2U-S4PC4tYYPSnAB%2F-LbD7qX9szv_yiVoeSCs%2Fstar4.PNG?alt=media\&token=c032f0f1-397d-4548-ae1b-476bb3a731bd)

{% code title="star1.py" %}

```python
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.pencolor('green')
for x in range(13):
        turtle.forward(200)
        turtle.left(150)
```

{% endcode %}

What happens if we try changing the range and the left angle?

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-LbD2U-S4PC4tYYPSnAB%2F-LbD8Wg6AXZY9VxQ657w%2Fstar5.PNG?alt=media\&token=55557fe8-c04d-483d-94be-a01950b093f7)

{% code title="star2.py" %}

```python
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.pencolor('purple')
for x in range(100):
        turtle.forward(200)
        turtle.left(175)
```

{% endcode %}

### Spiral Circle

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-LbDAfyyzPrBvS0oZ7Ne%2F-LbDB3CeYsu2t2nrjflN%2Fspiralcircle.PNG?alt=media\&token=3acd888a-4c30-4317-9ae3-b01c2c9e3fcd)

{% code title="spiral.py" %}

```python
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.pencolor('pink')
for i in range(180):
    turtle.forward(100)
    turtle.right(30)
    turtle.forward(20)
    turtle.left(60)
    turtle.forward(50)
    turtle.right(30)
    turtle.penup()
    turtle.setposition(0, 0)
    turtle.pendown()
    turtle.right(2)
```

{% endcode %}
