# Turtle Graphics with loops

## Shapes

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

### Square

![](/files/-LabBdbNmdjk0_vvqgdB)

{% 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()
```

![](/files/-Lae5cIM0RrtA9znIEga)

```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 %}

![](/files/-Lae8AfTjswlnkvkqy3H)

### Zigzag

### Star

![](/files/-LabCzFOqecNZOUehZ5d)

{% 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

![](/files/-LabFDl4ly9naFIKqq14)

{% 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 %}

![](/files/-LaeFyQY-Nx7yWcgnGLV)

{% 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 %}

![](/files/-LaeNNHjJyf9U_1MwXpn)

{% 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.

![](/files/-LbD7qX9szv_yiVoeSCs)

{% 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?

![](/files/-LbD8Wg6AXZY9VxQ657w)

{% 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

![](/files/-LbDB3CeYsu2t2nrjflN)

{% 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pythonclassroom.com/turtle-graphics/turtle-graphics-with-loops.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
