I need to create a function named "caterpillar()", this function is used to join my draw_circle() function, and draw_line() functions together to create a caterpullar. I'm stuck on trying to create the three circles needed for the caterpillars body using draw_circle within a while loop. This is all my code so far:
from turtle import *
import turtle
def moveto(x,y):
penup()
goto(x,y)
pendown()
def draw_circle(xpos,ypos,radius,colour):
moveto(xpos,ypos)
circle(radius)
turtle.fillcolor(colour)
def draw_line(x1, y1, x2, y2):
penup()
goto(x1,y1)
pendown()
goto(x2,y2)
def draw_square(x,y,length,colour):
moveto(x,y)
forward(length)
right(90)
forward(length)
right(90)
forward(length)
right(90)
forward(length)
turtle.fillcolor(colour)
def caterpillar():
draw_line(0,30,-20,-15) # feelers
draw_line(0,30,20,-15) # feelers
draw_line(60,30,40,-15) # feelers
draw_line(60,30,80,-15) # feelers
draw_line(120,30,100,-15) # feelers
draw_line(120,30,140,-15) # feelers
for _ in range(3) : # 3 body circles
xpos = 0
ypos = 0
radius = 30
turtle.begin_fill()
draw_circle(0,0,30,"green")
turtle.end_fill()
xpos = xpos + (radius*2)
caterpillar()
I am stuck on the last part under "for _ in range(3)" - I need to loop three circles using the draw_circle function at these specific coords: Caterpillar
I've been stuck on this for hours, any help would be much appreciated! Edit: Also forgot to mention, that i keep getting the error "File "C:\Users\Rekesh\Desktop\caterpillar\1.py", line 48, in caterpillar xpos = xpos +(radius*2) UnboundLocalError: local variable 'xpos' referenced before assignment When I use xpos = xpos, i'm not sure if this is needed.