Nested function has an issue with a variable

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()

When you assign to a variable in a function, Python assumes that the variable is local to that function unless it’s declared in the function as global or nonlocal. “global” means that the variable exists in the main code of the module (i.e. file) and “nonlocal” means that the variable exists in the enclosing function (where one function is defined inside another).

Thanks for the tip, that solved it!

More detail:

This is how to read: turnsLeftHEX=turnsLeftHEX-1

  1. Define “turnsLeftHEX” name in function scope. This overrides the same name in global scope.
  2. Decrement “turnsLeftHEX” uninitialized variable by 1 and assign it to “turnsLeftHEX”. BUG!!

You cannot decrement an uninitialized variable. An uninitialized variable is a variable that is declared but is not set to a definite known value before it is used .