Re: Error

Discussion in 'Python' started by MRAB, Nov 14, 2012.

  1. MRAB

    MRAB Guest

    On 2012-11-14 15:18, inshu chauhan wrote:
    >
    > for this code m getting this error :
    >
    > CODE :
    > def ComputeClasses(data):
    > radius = .5
    > points = []
    > for cy in xrange(0, data.height):
    > for cx in xrange(0, data.width):
    > if data[cy,cx] != (0.0,0.0,0.0):
    > centre = data[cy, cx]
    > points.append(centre)
    >
    >

    Look at this line:

    > change = True
    >

    It's indented the same as the preceding 'if' statement, which means
    that it's executed even if the body of the 'if' statement wasn't
    executed and it hasn't assigned to 'centre'.

    So 'change' has been set to True, the 'while' loop is entered, and
    subsequently an attempt is made to get 'centre', which hasn't been set.

    > while change:
    >
    > for ring_number in xrange(1, 1000):
    > change = False
    > new_indices = GenerateRing(cx, cy, ring_number)
    >
    >
    > for idx in new_indices:
    > point = data[idx[0], idx[1]]
    >
    > if point == (0.0, 0.0, 0.0 ):
    > continue
    > else:
    > dist = distance(centre, point)
    > if dist < radius :
    > print point
    > points.append(point)
    > change = True
    > print change
    >
    >

    The indentation of this line looks wrong to me:

    > break
    >

    It'll affect the 'for cx' loop at the end of its first iteration, every
    time.
    >
    > ERROR :
    > Traceback (most recent call last):
    > File "Z:\modules\classification2.py", line 74, in <module>
    > ComputeClasses(data)
    > File "Z:\modules\classification2.py", line 56, in ComputeClasses
    > dist = distance(centre, point)
    > UnboundLocalError: local variable 'centre' referenced before assignment
    >
    > And i am unable to understand .. WHY ?
    >
    MRAB, 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,656
  2. JavaQueries
    Replies:
    1
    Views:
    3,651
    John C. Bollinger
    Mar 1, 2005
  3. Balaji
    Replies:
    3
    Views:
    10,091
  4. Bishop
    Replies:
    1
    Views:
    771
    Bishop
    Feb 24, 2007
  5. juvi
    Replies:
    3
    Views:
    1,047
    Alexey Smirnov
    Jan 22, 2009
Loading...

Share This Page