# Nested function has an issue with a variable

**URL:** <https://discuss.python.org/t/nested-function-has-an-issue-with-a-variable/28253>\
**Category:** Python Help\
**Created:** [June 21, 2023, 3:49pm UTC](https://discuss.python.org/t/nested-function-has-an-issue-with-a-variable/28253 "2023-06-21T15:49:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kade](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@Kade](https://discuss.python.org/u/Kade)\
**Post date:** [June 21, 2023, 3:49pm UTC](https://discuss.python.org/t/nested-function-has-an-issue-with-a-variable/28253/1 "2023-06-21T15:49:34Z")

</div>

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:

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

```

 ![Screenshot 2023-06-21 12.23.13 PM](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/6/616c1a42b80ec2abd58df848a11e403b21bebb6e.png)

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [June 21, 2023, 4:27pm UTC](https://discuss.python.org/t/nested-function-has-an-issue-with-a-variable/28253/6 "2023-06-21T16:27:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kade](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@Kade](https://discuss.python.org/u/Kade)\
**Post date:** [June 21, 2023, 4:28pm UTC](https://discuss.python.org/t/nested-function-has-an-issue-with-a-variable/28253/7 "2023-06-21T16:28:54Z")

</div>

Thanks for the tip, that solved it!

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [June 21, 2023, 4:29pm UTC](https://discuss.python.org/t/nested-function-has-an-issue-with-a-variable/28253/8 "2023-06-21T16:29:58Z")

</div>

More detail:

> <https://stackoverflow.com/questions/370357/unboundlocalerror-trying-to-use-a-variable-supposed-to-be-global-that-is-rea>

> <https://stackoverflow.com/questions/8447947/is-it-possible-to-modify-a-variable-in-python-that-is-in-an-outer-enclosing-b>

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [June 21, 2023, 6:24pm UTC](https://discuss.python.org/t/nested-function-has-an-issue-with-a-variable/28253/9 "2023-06-21T18:24:39Z")

</div>

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**.
