Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error

19 views
Skip to first unread message

inshu chauhan

unread,
Nov 14, 2012, 10:18:38 AM11/14/12
to pytho...@python.org, Zero Piraeus

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)   
               

            change = True

            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
                 
               
            break


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 ?


Chris Angelico

unread,
Nov 14, 2012, 10:28:12 AM11/14/12
to pytho...@python.org
On Thu, Nov 15, 2012 at 2:18 AM, inshu chauhan <insid...@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.

ChrisA

Joel Goldstick

unread,
Nov 14, 2012, 11:02:34 AM11/14/12
to pytho...@python.org
On Wed, Nov 14, 2012 at 10:18 AM, inshu chauhan <insid...@gmail.com> 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):
This code is only run if the test above is true
                centre = data[cy, cx]
                points.append(centre)   
               

            change = True

            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)
centre is only set if the test above is true.  In your run, it apparently wasn't
                            if  dist < radius :
                                print point
                                points.append(point)
                                change = True
                                print change
                 
               
            break


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

unread,
Nov 14, 2012, 1:48:52 PM11/14/12
to pytho...@python.org
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.

inshu chauhan

unread,
Nov 20, 2012, 7:31:42 AM11/20/12
to pytho...@python.org
I did the following changes in this part of my programme.. now the refereence error is removed but its showing me another error :


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):
                continue
            else :
                centre = data[cy, cx]
                print centre
                points.append(centre)   
               

            change = True 

            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
                 
               
                break
       
           
            print points


ERROR :

Traceback (most recent call last):
  File "Z:/modules/classification1.py", line 71, in <module>
    ComputeClasses(data)
  File "Z:/modules/classification1.py", line 47, in ComputeClasses

    point = data[idx[0], idx[1]]
error: index is out of range

What is meant by this statement ' Index out of range ' ? Does it mean that my range 1, 1000 is exceeded ??



Dave Angel

unread,
Nov 20, 2012, 8:01:52 AM11/20/12
to inshu chauhan, pytho...@python.org
When you're using custom classes that mimic the standard ones, the error
can mean most anything. But assuming the design was to keep as close as
possible, it simply means that you're subscripting a list with an index
that's too large or too small. So if idx is a list that has only one
element, element number zero, then idx[1] would be out of range. On the
same line, if data is acting kind of like a two-dimensional list, then
it has limits on each dimension, and either idx[0] is too big/small for
the first dimension, or idx[1] is too big or small for the second.

First thing is to figure out which part of this expression is causing
the exception. So do a separate pair of assignments,
dummy0 = idx[0]
dummy1 = idx[1]

and then point = data[dummy0, dummy1]

Incidentally, if idx is a tuple or a list, of exactly two items, then
you could just say
point = data[*idx]

Anyway, if that still doesn't make things clear, then print dummy0 and
dummy1 before the point= line. That way you can see the last value, the
one it dies on, just before the stack trace. Naturally, you could also
print the size attributes of the data item as well.


--

DaveA

Mark Lawrence

unread,
Nov 20, 2012, 8:21:56 AM11/20/12
to pytho...@python.org
What makes you think the error applies to the value from the xrange
call? The traceback tells you that the error has occurred at line 47.
Looking at that and the lines above, I'd guess your problem lies in the
return values from the GenerateRing function.

--
Cheers.

Mark Lawrence.

Dave Angel

unread,
Nov 20, 2012, 8:23:34 AM11/20/12
to inshu chauhan, pytho...@python.org
On 11/20/2012 08:19 AM, inshu chauhan wrote:
> Yes you are rightI figured that out after posting to python list.. actually
> my index is reaching the last point.. and my prog is not doing what I
> want.. I am wondering why it is reaching the last point in my list .. its
> never stopping in between ???
>
(Please don't top-post)

If this is intended as a non-rhetorical question, a bit more specific
wording is needed. Which index is reaching what last point? And in
what loop is it skipping in-between values?


--

DaveA
0 new messages