Search code examples
pythonmorse-code

Need help making a Morse Code to Text Translator | Python


so I'm having some trouble making a morse code to text translator. I made the text to morse however, When I tried making morse to text, It didnt work out. I looked up online and since I'm new to python I couldnt really understand most of it so I decided to make one on my own. It works as long as there are no spaces, but when there are spaces, I get this error.

Text to Morse or Morse to Text
Please type ttm for text to morse or type mtt for morse to text.
mtt
What would you like to have be translated to English?
.... ..  . ...- . .-. -.-- --- -. .
hiTraceback (most recent call last):
 File "main.py", line 61, in <module>
   print(mtt_dict[words], end="")
KeyError: ''

I translated "hi everyone" and it didnt really work

Here's the code:

ttm_dict = { 'a':'.-', 'b':'-...', 
                    'c':'-.-.', 'd':'-..', 'e':'.', 
                    'f':'..-.', 'g':'--.', 'h':'....', 
                    'i':'..', 'j':'.---', 'k':'-.-', 
                    'l':'.-..', 'm':'--', 'n':'-.', 
                    'o':'---', 'p':'.--.', 'q':'--.-', 
                    'r':'.-.', 's':'...', 't':'-', 
                    'u':'..-', 'v':'...-', 'w':'.--', 
                    'x':'-..-', 'y':'-.--', 'z':'--..', 
                    '1':'.----', '2':'..---', '3':'...--', 
                    '4':'....-', '5':'.....', '6':'-....', 
                    '7':'--...', '8':'---..', '9':'----.', 
                    '0':'-----', ', ':'--..--', '.':'.-.-.-', 
                    '?':'..--..', '/':'-..-.', '-':'-....-', 
                    '(':'-.--.', ')':'-.--.-'}

mtt_dict = {'-.--.-':')' ,'.--.-':'('                    
 ,'-....-':'-' ,'.-..-':'/' ,'..--..':'?'                    
 ,'-.-.-.':'.' ,'--..--':' ,' ,'-----':'0'                    
 ,'.----':'9' ,'..---':'8' ,'...--':'7'                    
 ,'....-':'6' ,'.....':'5' ,'-....':'4'                    
 ,'--...':'3' ,'---..':'2' ,'----.':'1'                    
 ,'..--':'z' ,'--.-':'y' ,'-..-':'x'                    
 ,'--.':'w' ,'-...':'v' ,'-..':'u'                    
 ,'-':'t' ,'...':'s' ,'.-.':'r'                    
 ,'-.--':'q' ,'.--.':'p' ,'---':'o'                    
 ,'.-':'n' ,'--':'m' ,'..-.':'l'                    
 ,'-.-':'k' ,'---.':'j' ,'..':'i'                    
 ,'....':'h' ,'.--':'g' ,'.-..':'f'                    
 ,'.':'e' ,'..-':'d' ,'.-.-':'c'                    
 ,'...-':'b' ,'-.':'a'
}
question = input("Text to Morse or Morse to Text\nPlease type ttm for text to morse or type mtt for morse to text.\n")

#Text to Morse
if question == "ttm":
  encrypt_q = input("What would you like have be translated to Morse Code\n")
  encrypt = encrypt_q.lower()
  morse = "" 
  for letter in encrypt: 
    encrypt.lower()
    if letter != ' ': 

            morse += ttm_dict[letter] + ' '
    else: 

            morse += ' '
  print(morse) 
  #Morse to Text
elif question == "mtt":
  decrypt = input("What would you like to have be translated to English?\n")
  lenword = len(decrypt)
  words = ''
  for i in decrypt:
    if i != ' ':
        words=words+i
        if i not in mtt_dict:
            print('Data not formatted properly')
            break
    else:
        print(mtt_dict[words], end="")
        words = ''

    #If they are cannot read
else:
  print("Invalid option")

Any help will be appreciated


Solution

  • You must buil an encoder/decoder, al told by @Jolbas, your problem is to separate unequivocally words by characters .

    If the contract is single and double respectively for characters and words you can use split in this manner:

    <phrase>.split('  ') for word # 2 spaces
    <word>.split(' ') for characters # 1 space
    

    HIT:

    So all can be done using nested list comprehension

    [ [c for c in word] for word in phrase]
    

    Using this trick the big part is solved.

    This is a concise version (not all loves nested comprehension... anyway it's not too bad)

    The code

    ttm_dict = { 'a':'.-', 'b':'-...', 'c':'-.-.', 'd':'-..', 'e':'.',
                'f':'..-.', 'g':'--.', 'h':'....', 'i':'..', 'j':'.---', 'k':'-.-',
                'l':'.-..', 'm':'--', 'n':'-.', 'o':'---', 'p':'.--.', 'q':'--.-',
                'r':'.-.', 's':'...', 't':'-', 'u':'..-', 'v':'...-', 'w':'.--',
                'x':'-..-', 'y':'-.--', 'z':'--..', '1':'.----', '2':'..---', '3':'...--',
                '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.',
                '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-',
                '(':'-.--.', ')':'-.--.-'}
    
    mtt_dict = {v:k for k,v in ttm_dict.items()}
    question = input("Text to Morse or Morse to Text\nPlease type ttm for text to morse or type mtt for morse to text.\n")
    if question == "ttm":
      encrypt_q = input("What would you like have be translated to Morse Code\n")
      # ' ' (single space, char separator
      # '  ' (double space) word separator
      morse = '  '.join([ ' '.join([ttm_dict[c] for c in word]) for word in encrypt_q.lower().split(' ')])
      print(morse)
    
    elif question == "mtt":
      decrypt = input("What would you like to have be translated to English?\n")
      print(' '.join([''.join([mtt_dict[c] for c in word.split(' ')]) for word in decrypt.split('  ')]))
    
    else:
      print("Invalid option")
    

    Here the result:

    Please type ttm for text to morse or type mtt for morse to text.
    ttm
    What would you like have be translated to Morse Code
    ciao da glauco
    -.-. .. .- ---  -.. .-  --. .-.. .- ..- -.-. ---
    
    Please type ttm for text to morse or type mtt for morse to text.
    mtt
    What would you like to have be translated to English?
    -.-. .. .- ---  -.. .-  --. .-.. .- ..- -.-. ---
    ciao da glauco