2014-09-08 1 views
0

전역 변수를 사용하지만 함수가 사용할 수 없기 때문에 UnboundLocalError를주고 그 이유를 알고 싶습니다.UnboundLocalError : 할당 전에 로컬 변수 'pos'가 참조되었습니다.

내가 다음 코드를하려고 할 때 작동하고 나에게 오류를 제공하지 복용량, "UnboundLocalError : 지역 변수 'POS'할당하기 전에 참조는"pos가 글로벌 VAR 그렇게 때문에

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) 

잘 나는 그것을 얻을 해달라고 func가 액세스 할 수 없게 된 이유는 무엇입니까? 그리고 그 이유를 아는 것이 정말로 중요합니까?

다음 코드를 사용하여이 문제를 해결했지만 동일한 오류가 발생했습니다. 나는 그것을 얻지 못한다. 왜 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) 

을 가변적이고에도 불구하고 나는 누군가가 내가 그것을 넣어 필요한 생각하지만 경우는 아주 오래 전부터 모든 코드를 포함 didnt한다. pos는 외부 범위이기 때문에
그렇지 않은 : 설명이 pos

find_best_shifts_rec() 내부에 선언 단순히

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

또는 아래

global pos 

:

답변

0

어느 줄을 추가 인정하다 따라서 함수 변수로 간주되므로 액세스하기 전에 선언해야합니다.

+0

@ JohnFieldman global은 효율성과 아무 관련이 없습니다. 그리고 이미 전역 적 범위 내에서 선언했습니다.'global' 키워드를 추가하면 인터프리터에게 어디에서 찾을 지 알려줍니다. – alfasin

관련 문제