UnboundLocalError:局部变量'pos'在赋值前被引用
我在使用全局变量,但有一个函数却无法使用其中的一个,导致我遇到了“UnboundLocalError”的错误。我想知道这是为什么。
当我尝试下面的代码时,它无法正常工作,并给我这个错误:“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)
我不明白,因为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
def find_best_shifts_rec(wordlist, text,start=0):
你可以在下面添加这一行:
global pos
或者直接在 find_best_shifts_rec()
函数里面声明 pos
。
解释:
因为 pos
是在外部范围定义的,所以它无法被识别,因此被当作函数内部的变量来看待。所以在使用它之前,必须先声明它。