0

I am a python new comer. Right Now I am having some troubles with python's local variable setting.

Here is the thing, I got a mistake output called :

UnboundLocalError: local variable 'i1' referenced before assignment

By searching on the StackOverFlow, I now know that my fault was due to the functioning range of the local variable. For python is a dynamic language and doesn't have variable declaration. Is there anyone outer there who can help to fix that? I really need to use this variable after the iteration. Thanks a lot.(Although I know it can works, I prefer not to use the global declaration)

Here is my code, I use it as to fulfill the insertion sort.

def insert_sort(my_str):
    locf = len(my_str)
    for j1 in xrange(1,locf,1):
        ticket = 0
        temp = my_str[j1]
        for i1 in xrange(j1-1,0,-1):
            if i1>=0 and my_str[i1]<my_str[j1]:
                ticket  = 1
                my_str[i1+1] = my_str[i1]
            else:
                break
        if ticket == 0:
            my_str[i1] = temp
    return my_str

The mistake is :

  File "/Users/tk/PycharmProjects/pyal/Insertation Sort.py", line 34, in <module>
    print insert_sort(tk_list)
  File "/Users/tk/PycharmProjects/pyal/Insertation Sort.py", line 15, in insert_sort
    my_str[i1] = temp
UnboundLocalError: local variable 'i1' referenced before assignment

I know it is because i1 is within the for loop and cannot be used outsides. But what if I do need to do so? Is there any compensate methods?

Sure, I do can not to use the for loop and use while instead. But sometimes I do need to use a local variable within a loop outside its range. And I have already wrote it out using the while loop:

def insert_sort(my_str):
    locf = len(my_str)
    for j1 in xrange(1,locf,1):
        temp = my_str[j1]
        i1 = j1 -1
        while i1>=0 and my_str[i1]>temp:
            my_str[i1+1] = my_str[i1]
            i1 -= 1
        my_str[i1+1] = temp
    return my_str

Thanks you sincerely.

Tk_75963
  • 15
  • 3

1 Answers1

1

Actually you can use values assigned in a for loop definition outside that loop, e.g.:

for x in range(5):
    print x # 0 1 2 3 4
print x # still 4

In your code, however, when j1 == 1, (its first value) xrange(j1 - 1, 0, -1) is xrange(0, 0, -1), which is an empty iterator, so i1 is never assigned.

The minimal fix is to change how j1 is assigned:

for j1 in xrange(2, locf, 1):

Now i1 will get assigned for all values of j1.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Wow, Now I got it... But what about the functioning range of this variable. Now I just got a little bit of confused... – Tk_75963 May 04 '14 at 08:34
  • I don't understand what you mean by "functioning range". *Which* variable? – jonrsharpe May 04 '14 at 08:34
  • In you case is the x. When and where will it not working anymore... Will this variable keep alive all the way until the function itself is end? – Tk_75963 May 04 '14 at 08:37
  • Oh, I see. Yes, that will remain in scope until the function ends, retaining the last value it had in the loop (unless reassigned). – jonrsharpe May 04 '14 at 08:38
  • @Tk_75963 that's a sketchy-looking link; I'll take your word for it! – jonrsharpe May 04 '14 at 09:21
  • Ah, It is my personal blog. Right now I am trying to using python to fulfill every pseudocode in a book called 'Introduction to The Design and Analysis of Algorithm'. I registered the domain myself, so it looks not so nice. Sorry for the late reply... – Tk_75963 May 05 '14 at 08:43