Reputation:
I'll start by a simplified explanation of what my code (function) has to do:
I get a file with words in it that I have to code to morse code, so I have another file where each character has a morse code. My problem occurs when I want to put each word in a dictionary as a key with as value that morse code.
to get the pattern of the code and assign it to that word in my dictionary I use another function but it says 'UnboundLocalError: local variable 'patroon' referenced before assignment' while 'patroon' is actually my other function:
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
and here is the function in which it is called:
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
where the stars are is where the error occurs I'm new to python so I don't really know if this would be enough information.
this is my full code:
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
Upvotes: 1
Views: 708
Reputation:
I have found my mistake, apparently python isn't (yet) smart enough to keep the name patroon as function and the 'patroon' as variable in my for loop separately (which I later use in the same function 'groepen')
Upvotes: 0