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.
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
.