0

I wrote the following code to attempt to translate between English and Morse code:

#morse translator
eng={' ': '  ','A' or 'a':'.- ', 'B' or 'b':'-... ' ,'C' or 'c': '-.-. ' , 'D' or 'd' :'-.. ' , 'E' 
or 'e': '. ' ,'F' or 'f': '..-. ', 'G' or 'g':  '--. ' , 'H' or 'h':   '.... ',   'I' or 'i': '.. ',     
'J' or 'j': '.--- ',    'K' or 'k': '-.- ', 'L' or 'l': '.-.. ', 'M' or 'm': '-- ',   'N' or 'n'  : 
'-. ',   'O' or 'o':    '--- ', 'P' or 'p': '.--. ',    'Q' or 'q': '--.- ',    'R' or 'r': '.-. ', 
'S' or 's':    '... ',  'T' or 't': '- ',   'U' or 'u': '..- ', 'V' or 'v' :    '...- ','W' or 'w': 
'.-- ', 'X' or 'x': '-..- ', 'Y' or 'y':'-.-- ','Z' or 'z': '--.. '}
mrse={' ': ' ','.-':'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'}

while True:
    a=input("To convert English to morse, press E. To convert Morse to English, press M: ")
    if a=='E' or a=='e':
        b=(str.upper(input("Type your sentence to be converted : ")))
        for b in b:
            if b in eng:
                print(eng[b],end='')
        if b not in eng:
            print("Beep boop too dumb for numbers and special characters!")
            continue

    elif a=='M' or a=='m':
        c=(str.upper(input("Type your morse sentence to be converted : ")))
        if c in mrse:
            if ' ' not in c:
                print(mrse[c],end='')
        if ' ' in c:
            print("Beep boop still learning.... :p")
            #for c in c:
                #print(mrse[c],end='')
            #placeholder for when i figure it out hehe
        elif c not in mrse:
                print("Beep boop too dumb for numbers and special characters!")
                continue
    else:
        print("Enter either E or M !")
        continue
    
    d=input("\nContinue to more? Y/N : ")
    if d== 'Y' or d =='y':
        continue
    else:
        print("Program closing....")
        break
    

However, the output for translating from Morse code to English is incorrect.

Eg: an input of .... . should result in Hi, but instead results in EEEE E. It appears to translate each individual . separately.

How do I fix this?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Okay, so you know how you had to write `if a=='E' or a=='e':` in order to make it work properly, and how `if a=='E' or 'e'` does **not** work? Did you consider that maybe the same reasoning applies to creating the `eng` and `mrse` dictionaries? Although, there are simpler ways to go about treating input in a *case-insensitive* way. You might try using a search engine to figure that out, using that search term. – Karl Knechtel Apr 21 '21 at 13:03
  • Anyway, the problem you're *reporting* is that the morse code translation is looking at each *character* of the input text, instead of each *word*. Maybe you could try looking up how to `split` the input into words? Hint, hint. – Karl Knechtel Apr 21 '21 at 13:04
  • The first comment also applies somewhat to the first dictionary. A key will be evaluated, so `'A' or 'a'` is not going to be both at the same time. – trincot Apr 21 '21 at 13:08
  • I edited the question to remove chatter and ask the question directly, and to show the general style that is preferred here. Please keep in mind that Stack Overflow is *not a discussion forum*; we don't care about your background or life story, and everything we need to know about your skill level is already apparent in the code. – Karl Knechtel Apr 21 '21 at 13:09

1 Answers1

0

You can use split to identify the individual Morse words in your input:

sequence = input("Type your morse sentence to be converted : ")
for word in sequence.split():
    if word not in mrse:
        print("[Numbers and special characters not supported!]")
        continue
    print(mrse[word], end='')
trincot
  • 317,000
  • 35
  • 244
  • 286