Re: Error

Discussion in 'Python' started by Chris Angelico, Nov 14, 2012.

  1. On Thu, Nov 15, 2012 at 2:18 AM, inshu chauhan <> 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.

    ChrisA
     
    Chris Angelico, Nov 14, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hfk0
    Replies:
    2
    Views:
    21,688
  2. JavaQueries
    Replies:
    1
    Views:
    3,695
    John C. Bollinger
    Mar 1, 2005
  3. Balaji
    Replies:
    3
    Views:
    10,130
  4. Bishop
    Replies:
    1
    Views:
    801
    Bishop
    Feb 24, 2007
  5. juvi
    Replies:
    3
    Views:
    1,057
    Alexey Smirnov
    Jan 22, 2009
Loading...

Share This Page