Concentric Rainbow Squares


I want to make this image using turtle,

Picture of result

This is what I have so far, but there are multiple errors.

import turtle
import colorsys
import random

def draw_circle(x,y,r,color):
    turtle.up()
    turtle.seth(0)
    turtle.goto(x,y-r)
    turtle.down()
    turtle.fillcolor(color)
    turtle.begin_fill()
    turtle.circle(r)
    turtle.end_fill()

def draw_square(x,y):
    turtle.up()
    turtle.seth(0)
    turtle.goto(x,y)
    turtle.fillcolor(color)
    turtle.pencolor(color)
    turtle.down()
    turtle.begin_fill()

    for i in range(2):
        turtle.fd(w)
        turtle.left(90)
        turtle.fd(h)
        turtle.left(90)

    turtle.end_fill()


turtle.speed(0)

x = 0
y = -200

for i in range(100):
    color = colorsys.hsv_to_rgb(x,1,1)
    turtle.pencolor(color)
    draw_square(x,y)
    x += .01
    y += 10

I get this error:

Traceback (most recent call last):
  File "/Users/MBach/Documents/concentric squares.py", line 39, in <module>
    draw_square(x,y)
  File "/Users/MBach/Documents/concentric squares.py", line 24, in draw_square
    turtle.fd(w)
NameError: name 'w' is not defined
>>> 

Answers:


While waiting for @darksky's solution to complete ;-) I wrote this alternate example which uses stamping instead of drawing to simplify the code and speed up the graphics:

import turtle
import colorsys

STEP = 2  # distance between squares
WIDTH = 375  # width of the biggest square

CURSOR_SIZE = 20

def draw_square(width, color):
    turtle.shapesize(width / CURSOR_SIZE)
    turtle.pencolor(color)
    turtle.stamp()

turtle.shape('square')
turtle.fillcolor('white')

hue = 0

for width in range(WIDTH, 0, -2 * STEP):
    color = colorsys.hsv_to_rgb(hue, 1, 1)
    draw_square(width, color)
    hue += 0.01

turtle.done()

Stamping isn't the solution to all turtle problems but simple geometric drawings like this, it has its advantages:

enter image description here