0
def partial(f,h=0.0001):
    #checker
    x=y=z=1
    fx=(f(x+h,y,z)-f(x,y,z))/h
    fy=(f(x,y+h,z)-f(x,y,z))/h
    fz=(f(x,y,z+h)-f(x,y,z))/h
    if(fx==0):
        p=0
    elif(fy==0):
        q=0
    elif(fz==0):
        r=0
    fx=fy=fz=0
    a=15
    c=5
    for m in range (-a,a,c):
        for n in range (-a,a,c):
            for o in range (-a,a,c):
                x=m
                y=n
                z=o
                if(p==0):
                    x=0
                elif(q==0):
                    y=0
                elif(r==0):
                    z=0
                fx=(f(x+h,y,z)-f(x,y,z))/h
                fy=(f(x,y+h,z)-f(x,y,z))/h
                fz=(f(x,y,z+h)-f(x,y,z))/h
                arrow(pos=vector(m,n,o),axis=vector(+fx,+fy,+fz),color=color.red)
                print z
    print fx,fy,fz
    return 0

Where am I going wrong? I have declared p before, but it says p is referenced before assignment.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Kabir Thakur
  • 1
  • 1
  • 5
  • 1
    Your constant question is answered elsewhere: [Creating constant in Python](http://stackoverflow.com/q/2682745) – Martijn Pieters Apr 26 '15 at 17:16
  • 2
    You don't define `p` when `fx==0` is false. – Martijn Pieters Apr 26 '15 at 17:16
  • 1
    tried putting the else statement to give a value to p when fx is not 0. Still the same error – Kabir Thakur Apr 26 '15 at 17:23
  • Just set `p` to a value *always*. – Martijn Pieters Apr 26 '15 at 17:29
  • I'd set `p`, `q`, and `r` to -1, then run the if statements to set one of them to 0, as all three of those variables run the risk of being undefined in the for loop. – MasterOdin Apr 26 '15 at 17:30
  • It looks like you are using `p`, `q` and `r` as boolean flags. Just set them as such: `p = fx != 0`. Then `p` is either `True` (`fx` was not 0) or `False` (`fx` is 0), and later on use `if p:`. – Martijn Pieters Apr 26 '15 at 17:31
  • thank you @MasterOdin. I tried what you said and it worked. – Kabir Thakur Apr 26 '15 at 17:39
  • @ Martijn Pieters - do you want to answer the question more fully, not in teh comments - it has left this question semi-orphaned...the OP says you solved the problem and it is clearly exactly what you said by a quick glance at their code...but until it is answered it will come up in the review queue, etc... – DrCord Apr 26 '15 at 17:42
  • @KabirThakur, I typed up my comment to be a bit more specific. Feel free to accept if it helped! :) – MasterOdin Apr 26 '15 at 18:34

1 Answers1

0

You potentially use p, q, and r in your for loop in if/elif statements. That means that these have to be defined, or else you run the risk of the "UnboundLocalError: local variable '_' referenced before assignment" error popping for any of them. However, you only define one of these when you call the function depending on what fx, fy, and fz is).

The easiest solution is to just add this line:

p = q = r = -1

above your if statements setting one of them to 0. (around line 6)

Now, they'll all be defined and you can reference them inside your for loops regardless of which one gets set to 0.

MasterOdin
  • 7,117
  • 1
  • 20
  • 35