简体   繁体   中英

Python Morse Code: S.O.S

I am having trouble finishing this code. My original plan was to accept a long string as input , for example '... ---.. -. -.. .... . .-.. .--.' and then be able to use morse_code = [code_input.split(' ')] to separate them and run them individually but I am either getting the first character returning from the index or no return at all so can anyone help me mend this or help lead me in the way of a simpler solution? Thanks to all for any help!

#morse code converter
def main():

    code_input = get_input()
    morse_code(code_input)

def get_input():

    return input('Enter morse code: ')

def morse_code(code_input):

    morse_code = [code_input]

    convert = ['--..--', '.-.-.-', '..--..', '-----', '.----', '..---', '...--', '....-', '.....', '-....', '--...', '---..',
               '----.', '.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---' ,'-.-', '.-..', '--', '-.', '---',
               '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.-', '--..']
    new = [',', '.', '?', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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',]
    print(morse_code)
    length = len(morse_code)
    for x in range(length):
        for value in range(39):
            if morse_code[x] == convert[value]:
                print(new[value])

main()

Your idea to use split should work just fine:

>>> '... ---.. -. -.. .... . .-.. .--.'.split()
['...', '---..', '-.', '-..', '....', '.', '.-..', '.--.']

For the translation, I would use a dictionary instead of a list:

morse2text = {'...': 's', '---': 'o'}

To avoid key errors, I would a do a "safe lookup" using the dictionary get() method:

print( morse2text.get(m, m) )

Here is all the code put together (though with an incomplete dictionary) in case you want to build-off of a working example:

morse2text = {
    '.-': 'a',
    '-..': 'd',
    '.': 'e',
    '....': 'h',
    '..': 'i',
    '-.': 'n',
    '---': 'o',
    '...': 's',
    '-': 't',
}

s = '... ---.. -. -.. .... . .-.. .--.'
for m in s.split():
    print( morse2text.get(m, m) )

This should work for you:

def morse_code(code_input):
    morse_codes = code_input.split(' ')
    convert = ['--..--', '.-.-.-', '..--..', '-----', '.----', '..---', '...--', '....-', '.....', '-....', '--...', '---..',
               '----.', '.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---' ,'-.-', '.-..', '--', '-.', '---',
               '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.-', '--..']
    new = [',', '.', '?', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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',]
    print(morse_codes)
    code = ''
    for x in morse_codes:
        index = convert.index(x)
        code+=new[index]
    return code

print morse_code('... ---.. -. -.. .... . .-.. .--.')

Output:

['...', '---..', '-.', '-..', '....', '.', '.-..', '.--.']
'S8NDHELP'

As the Legendary Raymond mentioned, dict type is a better option.

Try dict(zip(convert,new)) to make a dictionary for conversion.

d = {'--..--': ',', '....-': '4', '.....': '5', '-...': 'B', '-..-': 'X',
 '.-.': 'R', '--.-': 'Q', '--..': 'Z', '.--': 'W', '..---': '2',
 '.-': 'A', '..': 'I', '-.-.': 'C', '...--': '3', '-': 'T', '.': 'E', 
 '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', '.----': '1',
 '.--.': 'P', '-----': '0', '-.-': 'Y', '-..': 'D', '----.': '9', 
 '-....': '6', '.---': 'J', '---': 'O', '.-.-.-': '.', '--': 'M',
 '-.': 'N', '....': 'H', '---..': '8', '...-': 'V', '--...': '7',
 '--.': 'G', '..-.': 'F'}

To use the dict:

translation = ''
for c in morse_codes:
    translation += d[c] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM