# I need help solving this strange variable scope error

**URL:** <https://discourse.processing.org/t/i-need-help-solving-this-strange-variable-scope-error/24929>\
**Category:** Processing.py\
**Created:** [October 26, 2020, 5:16pm UTC](https://discourse.processing.org/t/i-need-help-solving-this-strange-variable-scope-error/24929 "2020-10-26T17:16:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![monster\_master10](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@monster\_master10](https://discourse.processing.org/u/monster_master10)\
**Post date:** [October 26, 2020, 5:16pm UTC](https://discourse.processing.org/t/i-need-help-solving-this-strange-variable-scope-error/24929/1 "2020-10-26T17:16:30Z")

</div>

So, we all know the common error “local variable ‘yVel’ referenced before assignment”. I got this error, but this time it is different. I am referencing my “yVel” variable outside of the processing draw function, and inside the processing draw function I am saying “yVel += gravity”. I don’t know how to fix this because I cant put the variable in the draw function, because then it would just keep resetting the variable. And yes, I did try using python globals, and those didn’t work either. And, just to test, I defined the variable in the draw function, and it didn’t show another reference error for the “gravity” variable, so I know I can reference variables outside of the draw function. Here is my script:

```auto
width = 1000
height = 1000
#Variables
x = random(0,width)
y = random(0,height)
xVel = random(-10,10)
yVel = random(-0.5,0.5)
gravity = random(0,1)
elasticity = random(0,1)
radius = random(2.5,50)
friction = random(0,1)    
    

def draw():
    yVel += gravity
    x += xVel
    y += yVel
    if x<float(radius/2) or x>width-float(radius/2):
        xVel =- xVel*elasticity
    if x<float(radius/2) or x>width-float(radius/2):
        xVel =- xVel*elasticity
    if x < float(radus/2):
       x = float(radius/2)
    
    else:
        if x > width-float(radius/2):
            x = width-float(radius/2)
    
    if y < float(radus/2):
        y = float(radius/2)
        
    else:
        if y > width-float(radius/2):
            y = width-float(radius/2)
                            
    if y==height-float(radius/2):
        xVel *= friction
    
    ellipse(x, y, radius, radius)

```

Does anyone know why this is happening?

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [October 26, 2020, 5:19pm UTC](https://discourse.processing.org/t/i-need-help-solving-this-strange-variable-scope-error/24929/2 "2020-10-26T17:19:55Z")

</div>

i just realized why, you didnt define setup(), for some reason if you dont define that variables break.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 26, 2020, 6:30pm UTC](https://discourse.processing.org/t/i-need-help-solving-this-strange-variable-scope-error/24929/3 "2020-10-26T18:30:47Z")

</div>

> [@monster\_master10](#):
>
> … and inside the processing draw function I am saying “yVel += gravity”.

`global x, y, xVel, yVel, radius`

> **[Globals \\ Language (API)](https://py.processing.org/reference/globals.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.
