EDIT: Completely fixed now.
EDIT: Okay so now I got it to change '... --- ...' to 'sos' (yippee!) But for some reason, it gives me a KeyError when I input '... --- ... / ... --- ...'. This should give me 'sos sos' but I get a KeyError for ' '. I think this is because after the '/', there's a space which the program sees as a trigger to call the dictionary value of the current key. Problem being, the current key at that time is blank since the previous character was the '/'. How would I be able to fix this? Thanks.
I'm pretty new to programming with python (well, programming in general, really...) and today I had the bright idea of making a morse code converter.
I have the "plain text to morse code" working flawlessly, but the "morse code to plain text"?
Not so much.
The problem is that when I run the program, it doesn't give me anything, it just breaks its loop (like I told it to) without anything coming back to me.
If you guys could help me out, I'd very much appreciate it.
Oh also, 'decoding_dict' is the dictionary which I made which correlates morse code values to plain text. For example,
decoding_dict = {'...' : 's' , '---' : 'o'}
And so on and so forth.
def decode(text):
text += ' '
#I have it set up to trigger when there's a space, hence this.
global key
global decoded_text
#I thought maybe this would fix it, it didn't. :(
key = ''
decoded_text = ''
for i in text:
if i == '.':
key += i #This adds a '.' to the key to reference later.
continue
elif i == '-':
key += i #See above comment.
continue
elif i == ' ':
decoded_text += decoding_dict[key]
text = text[(len(key) + 1) :]
key = '' #Calls the value of the key, cuts out the used text, and resets key.
continue
elif i == '/':
decoded_text += decoding_dict['/']
continue #In morse code, a '/' is a ' ' and that's in the dict.
elif text == '':
print "Result: " + decoded_text
break #This is basically the end of the loop
else:
print "Error, please try again."
break
Now when I run it with '... --- ...' , it goes back to the beginning and doesn't print anything. (By beginning, I mean the menu I made beforehand.)