Question: I don't know why when `a` is located in `def test()` it can not be found and gives the error

> 
> UnboundLocalError: cannot access local variable 'a' where it is not associated with a value
> 
> 
> 

```py
import keyboard
import time

a = 0

def test():
    a+= 1
    print("The number is now ", a)
    time.sleep(1)

while keyboard.is_pressed('i') == False:

    test()

```

I tried setting `a` as `global a` or using a `nonlocal` modifier on it inside the `def` but it doesn't seem to work. Is there a way I can get it to recognize `a` and run properly?

Answer: 
To access a global variable within a function you must specify it with global. Otherwise, the variable inside the function is a local variable that only has utility inside that function. That's why you get the error: "UnboundLocalError: local variable 'a' referenced before assignment". Inside the function you haven't defined the variable 'a' yet, either local or global.

```
import keyboard
import time

a = 0

def test():
    global a
    a+= 1
    print("The number is now ", a)
    time.sleep(1)

while keyboard.is_pressed('i') == False:

    test()

```
You need to pass a to the function test. Python thinks as the `a` in the function as a local variable.
This will work:

```
import keyboard
import time
a = 0
def test(a=a):
    a+= 1
    print("The number is now ", a)
    time.sleep(1)
while keyboard.is_pressed('i') == False:
    test()

```