I’m messing around with making some art with Turtle Graphics, and I made a nested function with a variable to create a shape when it’s called. I first tried to create a variable and assign it a value of 60 in the function, and also tried moving it out of the function, as it keeps giving me the error “defined in enclosing scope referenced before assignment” and I don’t know why. I am fairly new to Python and don’t know much so any help would be appreciated.
Thanks!
Here is my code:
import turtle
turnsLeftHEX=60
def drawSpiral():
turtle.clearscreen
turtle.bgcolor('black')
turtle.right(angle=90)
turtle.goto(0,100)
turtle.color('red')
def THD():
turtle.forward(distance=170)
turtle.right(angle=124)
turnsLeftHEX=turnsLeftHEX-1
while turnsLeftHEX>0:
THD()
turtle.color('orange')
THD()
turtle.color('yellow')
THD()
turtle.color('green')
THD()
turtle.color('blue')
THD()
turtle.color('purple')
THD()
turtle.color('red')
drawSpiral()