我在完成此代碼時遇到問題。我最初的計劃是接受一個長字符串作爲輸入,例如'... --- .. - 。 - .. .... ...-。'然後能夠使用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請記住接受答案! –