---
# System prepended metadata

title: Python 4.1

---

---
title: Python 4.1
tag: python
---
```python=
import turtle

SIDE = 20
DISTANCE = 3


def square(t, side, color='pink'):
    t.begin_fill()
    t.fillcolor(color)
    for i in range(4):
        t.forward(side)
        t.left(90)
    t.end_fill()


def move(t, step):
    t.penup()
    t.left(90)
    t.forward(step)
    t.right(90)
    t.penup()


def move_left(t):
    t.penup()
    t.left(180)
    t.forward(DISTANCE)
    t.right(90)
    t.penup()


def calculate_color_change(index, size, call_index):
    first_bar_size = size + call_index
    total_size = sum(x for x in range(1, first_bar_size + 1))
    drawn_bars_size = sum(x for x in range((first_bar_size - call_index + 1), first_bar_size + 1))
    return ((index + 1) + drawn_bars_size)/total_size


def draw_spiral(t, size, call_index=0):
    for i in range(size):
        square(t, SIDE, [0.9, 0.45, calculate_color_change(i, size, call_index)])
        if i != size - 1:
            move(t, SIDE + DISTANCE)
        else:
            move_left(t)
            if size > 1:
                draw_spiral(t, size - 1, call_index + 1)


def run():
    t = turtle.Turtle()
    turtle.bgcolor(0.12, 0.1, 0.15)
    t.speed('fastest')
    t.penup()

    draw_spiral(t, 16)

    turtle.done()


run()
```


