0

I use global variables but for a reason a function cant use one of them and give me that UnboundLocalError, and i would like to know that reason.

when i try the next code it dose not work and give me this error,"UnboundLocalError: local variable 'pos' referenced before assignment"

lst_tubles=[]
pos=0
def find_best_shifts_rec(wordlist, text,start=0):
   """
    wordlist: list of words
    text: scambled text to try to find the words for
    start: where to start looking at shifts
    returns: list of tuples.  each tuple is (position in text, amount of shift)
    """


    def count_words(splited_text):
        count=0
        for i in range(len(splited_text)):
            if is_word(wordlist,splited_text[i]):
                count+=1
            else:
                break
        return count

    def find_shift(text):
        shift=0
        max_words=0
        while shift<27:
            splited_text=apply_shift(text,shift).split()
            valid_words=count_words(splited_text)
            if valid_words>max_words:
                max_words=valid_words
                best_shift=shift

            shift+=1

        return best_shift

    def go_to(text):
        move=0
        split=text.split()
        for word in split:
            if is_word(wordlist,word):
                move+=len(word)+1
            else:
                break
        return move

    text=text[start:]
    if text=='' or text==' ':
        return lst_tubles
    else:

        shift=find_shift(text)

        lst_tubles.append((pos,shift))
        text=apply_shift(text,shift)

        start=go_to(text)
        pos+=go_to(text)

        return find_best_shifts_rec(wordlist,text,start)


text='eqorqukvqtbmultiform wyy ion'
shift=find_best_shifts_rec(wordlist,text,)
print shift
print apply_shifts(text,shift)

well i dont get it since pos is global var so how does it come that the func cant access it!!?and its really important for me to know why?

i tried fixing that problem using the following code,but i got the same error. i just dont get it, why it isnt able to access the pos variable!! even though its easily accessing the lst_tubles varaible

def find_best_shifts(wordlist, text):
    """ Given a scrambled string, returns a shift key that will decode
    the text to words in wordlist, or None if there is no such key.
    """



    lst_tubles=[]
    pos=0
    return find_best_shifts_rec(wordlist,text,)





def find_best_shifts_rec(wordlist, text,start=0):
   """
    wordlist: list of words
    text: scambled text to try to find the words for
    start: where to start looking at shifts
    returns: list of tuples.  each tuple is (position in text, amount of shift)
    """


   def count_words(splited_text):
        count=0
        for i in range(len(splited_text)):
            if is_word(wordlist,splited_text[i]):
                count+=1
            else:
                break
        return count

   def find_shift(text):
        shift=0
        max_words=0
        while shift<27:
            splited_text=apply_shift(text,shift).split()
            valid_words=count_words(splited_text)
            if valid_words>max_words:
                max_words=valid_words
                best_shift=shift

            shift+=1

        return best_shift

   def go_to(text):
        move=0
        split=text.split()
        for word in split:
            if is_word(wordlist,word):
                move+=len(word)+1
            else:
                break
        return move

   text=text[start:]
   if text=='' or text==' ':
        return lst_tubles
   else:

        shift=find_shift(text)

        lst_tubles.append((pos,shift))
        text=apply_shift(text,shift)

        start=go_to(text)
        pos+=go_to(text)

        return find_best_shifts_rec(wordlist,text,start)


text='eqorqukvqtbmultiform wyy ion'
shift=find_best_shifts(wordlist,text)
print shift
print apply_shifts(text,shift)

i didnt include all of the code since its quite long,however if anyone believe its needed i will put it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

Either add the line:

global pos

below:

def find_best_shifts_rec(wordlist, text,start=0):

or simply declare pos inside find_best_shifts_rec()

Explanation:
Since pos is from an outer scope it's not recognized and hence considered as a function variable and as such - it should be declared before it's accessed.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • @JohnFieldman global has nothing to do with efficiency. And you already declared it in the global scope, adding the `global` keyword just tells the interpreter where to look for it. – Nir Alfasi Sep 08 '14 at 07:11