我在完成此代码时遇到问题。我最初的计划是接受一个长字符串作为输入,例如'... --- .. - 。 - .. .... ...-。'然后能够使用morse_code = [code_input.split('')]来分离它们并单独运行它们,但是我要么从索引返回第一个字符,要么没有返回,所以任何人都可以帮助我修补或帮助以更简单的解决方案带领我? 感谢所有的帮助!Python莫尔斯码:S.O.S
#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()
这看起来很干净。谢谢你的建议! – ccbrantley
@ccbrantley欢迎您! –
@ccbrantley请记住接受答案! –