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?