Morsecode Translator

Morsecode Translator - student project

The cypher thing really caught my interest so I decided to make a morsecode translator. The thing is, I don't know yet how to do it in reverse (i.e. i type in morsecode like ".... . .-.. .-.. ---    .-- --- .-. .-.. -.. " and it will give me the latin alphabet translation: "hello world"). This just takes latin alphabet inputs and returns morsecode. Sorry for terrible formatting, I don't know how to attach .py files here, but if I will learn it in the future, I will repost this. I just think this is worthy of sharing.

 

morsecode = {
'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' : '--.. ',
'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' : '----- ',
'.' : '.-.-.- ',
',' : '--..-- ',
'?' : '..--.. ',
' ' : ' '
}

def morse(orig):
text=""
for letter in orig:
new_letter = morsecode[letter]
text = text new_letter
return text

text = input("Write here: ")
result = morse(text)
print(result)

 

Each space, I made it return three spaces ' ' = '   ', then there is one space after each letter because it would be hard to read without them. 

 

"dance" in morsecode without space after each letter would be -...--.-.-.. and would look like "dpce", because ".-" (a) before "-." (n) looks like ".--."(p).

 

I added the option to be not case sensitive, so "happy birthday 2 u", "hAPpy BIrthdaY 2 u" and "happy birthday 2 U" will all return: .... .- .--. .--. -.--    -... .. .-. - .... -.. .- -.--    ..---    ..-