UnboundLocalError:赋值前引用的局部变量'pos'

时间:2014-09-08 06:56:07

标签: python python-2.7

我使用全局变量,但由于某种原因,函数无法使用其中一个并给我UnboundLocalError,我想知道这个原因。

当我尝试下一个代码时它不起作用并给我这个错误," UnboundLocalError:局部变量' pos'在分配之前引用"

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)

嗯,我没有得到它,因为pos是全局变量,所以它是怎么来的,功能不能访问它!!?它对我来说真的很重要吗?

我尝试使用以下代码修复该问题,但我得到了同样的错误。 我只是不明白,为什么它不能访问pos变量!即使它很容易访问lst_tubles变量

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)

我没有包含所有代码,因为它很长,但如果有人相信它需要我会把它。

1 个答案:

答案 0 :(得分:0)

添加以下行:

global pos

下面:

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

或在pos

中简单声明find_best_shifts_rec()

<强>解释
由于pos来自外部范围,因此无法识别它,因此被视为函数变量,因此应在它被访问之前声明。