Python摩尔斯电码:S.O.S

Python摩尔斯电码:S.O.S,python,algorithm,parsing,search,Python,Algorithm,Parsing,Search,我很难完成这个代码。我最初的计划是接受一个长字符串作为输入,例如“…--…--…”然后可以使用morse_code=[code_input.split(“”)]将它们分开并单独运行,但我要么从索引中返回第一个字符,要么根本不返回,那么有人能帮我修复这个问题,或者帮我找到一个更简单的解决方案吗? 谢谢大家的帮助 #morse code converter def main(): code_input = get_input() morse_code(code_input) de

我很难完成这个代码。我最初的计划是接受一个长字符串作为输入,例如“…--…--…”然后可以使用morse_code=[code_input.split(“”)]将它们分开并单独运行,但我要么从索引中返回第一个字符,要么根本不返回,那么有人能帮我修复这个问题,或者帮我找到一个更简单的解决方案吗? 谢谢大家的帮助

#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()
您使用split的想法应该很好:

>>> '... ---.. -. -.. .... . .-.. .--.'.split()
['...', '---..', '-.', '-..', '....', '.', '.-..', '.--.']
对于翻译,我会使用字典而不是列表:

morse2text = {'...': 's', '---': 'o'}
为了避免关键错误,我将使用dictionary方法进行“安全查找”:

以下是所有代码(尽管有一个不完整的字典),以防您想在工作示例的基础上构建:

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

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

这应该适合您:

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('... ---.. -. -.. .... . .-.. .--.')
输出:

['...', '---..', '-.', '-..', '....', '.', '.-..', '.--.']
'S8NDHELP'
正如传奇人物雷蒙德所提到的,dict类型是一个更好的选择

尝试
dict(zip(convert,new))
制作转换字典

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'}
要使用dict:

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

虽然.split()可以将字符串分开,但由于没有返回值,因此在运行时似乎出现了问题?不管。。。摩尔斯电码=电码输入。split()有效。这不起作用,摩尔斯电码=[code\u input.split()]。我已经试着补救了好几个小时了谢谢你!这看起来干净多了。谢谢你的建议!
translation = ''
for c in morse_codes:
    translation += d[c]