rosuav at gmail.com
Wed Nov 14 16:28:12 CET 2012
On Thu, Nov 15, 2012 at 2:18 AM, inshu chauhan <insideshoes at gmail.com> wrote:
> for this code m getting this error :
> CODE :
> def ComputeClasses(data):
> if data[cy,cx] != (0.0,0.0,0.0):
> centre = data[cy, cx]
> dist = distance(centre, point)
> ERROR :
> UnboundLocalError: local variable 'centre' referenced before assignment
> And i am unable to understand .. WHY ?
In brief, here's what causes that error:
1) Somewhere in the function, you assign to that name, which
implicitly sets it to be a local variable. That happens there where
you go "centre = data[cy, cx]".
2) Somewhere else in the function, you reference that name. That
happens where you try to calculate the distance from your
previously-defined centre to the current point.
3) At run-time, you haven't executed #1, but you do execute #2.
Your problem here I can't diagnose, but it looks like your first point
is (0.0,0.0,0.0), so centre never gets set. There are a couple of
possible fixes for this, and you'll need to figure out what to do
based on knowing your own code. Possibly you just need to initialize
centre above the loop, so that it always has a valid value; or
possibly the code below needs to not execute if the current centre
hasn't been set.
Go through your function's logic by hand and figure out what happens
when, and whether that's what it ought to do. Then decide what should
happen when a data value is (0.0,0.0,0.0) - currently it's retaining
the value of centre from the previous iteration of the loop, which
smells wrong to me.
Beyond that, I don't think I can really help, it's up to you.
More information about the Python-list