Search code examples
pythonloopsdictionaryif-statementmorse-code

Trying to translate Morse code into English


I am trying to translate Morse code into English with a dictionary I used. I am trying to have it where a user inputs a message and it will print his/her message into Morse code.

I found it easy to translate into Morse code, however Morse code to English gives me some issues.

First, if I type '.-' for 'A' I actually get 'E' instead as it's reading the first '.' key and translating it into 'E' instead of taking the whole string.

This is what I have tried so far :)

#If user types 'N' or 'n' it proceeds this loop
if morse_message == 'N' or morse_message == 'n':    #Loops looks to see if morse_message was 'N' or 'n'
    nomorse = input("Enter your Morse code here:")
    nomorse_list = (nomorse.join('')
    for letter in nomorse_list:
        not_morse = (morse_eng_dict[letter.upper()])
        print(not_morse)

And this is my dictionary I have

morse_eng_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"}

I would like to have Python take my Morse code and print the message back into English. Such as something like .. . .-.. .-.. --- (Which is HELLO)

This is for a project, so I can't use any fancy modules or anything. Any ideas or input? Thank you!


Solution

  • Something like this should work: Basically, it splits the string when ever there's a space, making a list in which each item is a morse code letter. It then checks each letter against the dictionary and takes the english counterpart. Lastly, it puts all these into a list, turns it into a string again and prints it. Hope it helps!

    morse_eng_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"}
    
    nomorse = input("Enter your Morse code here:")
    nomorse_list = nomorse.split() #this splits the string up wherever there is a space
    not_morse = []
    morse = True    #The code is morse (so far)
    for letter in nomorse_list:
        eng_letter = False
        for key in morse_eng_dict.keys():   #for each of the morse code letters in the dictionary
            if letter == key:
                eng_letter = morse_eng_dict[key]
        if eng_letter: #if a letter was found that corresponds
            not_morse.append(eng_letter)
        else:
            print("Input is not valid morse code.")
            morse = False
    if morse == True:
        string = "".join(not_morse) #joining the string together (without spaces in between)
        print(string)