UnboundLocalError:局部变量' patroon'在分配之前引用

时间:2015-11-26 16:16:26

标签: python-3.x

我首先简要解释一下我的代码(函数)必须做什么:

我得到一个包含文字的文件,我必须编写代码来编写morse代码,所以我有另一个文件,其中每个字符都有一个莫尔斯代码。当我想把字典中的每个单词作为一个具有莫尔斯代码值的密钥时,我的问题就出现了。

获取代码的模式并将其分配给我的字典中的单词我使用另一个函数,但它说“UnboundLocalError:local variable' patroon'在转让前引用' 虽然' patroon'实际上是我的另一个功能:

def patroon(woord, morse_dict, complement = False, spiegel = False):
    patroon_str = ''
    for letter in woord:
        patroon_str += morse_dict[letter.upper()]
    i=0

    if complement==True:
        patroon_list = list(patroon_str)
        for char in patroon_str:
            if char == '.':
                patroon_list[i]='-'
            elif char == '-': patroon_list[i]='.'
            i+=1
        patroon_str = ''.join(patroon_list)
    if spiegel == True:
        patroon_str = patroon_str[::-1]
    return patroon_str

以下是调用它的函数:

def groepen(woordenloc, morseloc):
    morse_dict = morsecodes(morseloc)
    woordpatroon_dict = {}
    woorden = open(woordenloc, 'r')

    for woord in woorden:
        ***woordpatroon_dict[woord] = patroon(woord, morse_dict)***
    patroonwoorden_dict = {}
    for woord, patroon in woordpatroon_dict.items():
        if patroon in patroonwoorden_dict:
            patroonwoorden_dict[patroon].add(woord)
        else:
            patroonwoorden_dict[patroon] = {woord}
    return patroonwoorden_dict

星星是发生错误的地方  我是python的新手,所以我不知道这是否足够了。

这是我的完整代码:

def morsecodes(locatie):
    morse_file = open(locatie, 'r')
    morse_dict = {}
    for line_str in morse_file:
        new_l = line_str.split()
        if new_l[0].isalpha:
            morse_dict[new_l[0].upper()] = new_l[1]
        else: morse_dict[new_l[0]] = new_l[1]
    return morse_dict

def patroon(woord, morse_dict, complement = False, spiegel = False):
    patroon_str = ''
    for letter in woord:
        patroon_str += morse_dict[letter.upper()]
    i=0

    if complement==True:
        patroon_list = list(patroon_str)
        for char in patroon_str:
            if char == '.':
                patroon_list[i]='-'
            elif char == '-': patroon_list[i]='.'
            i+=1
        patroon_str = ''.join(patroon_list)
    if spiegel == True:
        patroon_str = patroon_str[::-1]
    return patroon_str

def isomorse(woord1, woord2, morse_dict, complement = False, spiegel = False):
    patroon1 = patroon(woord1, morse_dict)
    patroon2 = patroon(woord2, morse_dict, complement, spiegel)
    if patroon1 == patroon2: return True
    else: return False

def groepen(woordenloc, morseloc):
    morse_dict = morsecodes(morseloc)
    woordpatroon_dict = {}
    woorden = open(woordenloc, 'r')

    for woord in woorden:
        woordpatroon_dict[woord] = patroon(woord, morse_dict)
    patroonwoorden_dict = {}
    for woord, patroon in woordpatroon_dict.items():
        if patroon in patroonwoorden_dict:
            patroonwoorden_dict[patroon].add(woord)
        else:
            patroonwoorden_dict[patroon] = {woord}
    return patroonwoorden_dict

1 个答案:

答案 0 :(得分:0)

我发现了我的错误,显然python还不够聪明,不能将名称patroon作为函数保存,并且'patroon'作为变量在我的for循环中单独使用(后来我在同一个函数中使用'groepen' )