无需使用字典即可发送莫尔斯电码

问题描述 投票:0回答:2

我正在尝试编写一个使用索引将文本转换为莫尔斯电码的程序。我不会使用字典。到目前为止我的代码看起来像这样:

#Set up variables

alpha = ['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']

morse = ['.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..']

#Get input from user

phrase = input('Enter a sentence to be converted to Morse Code')
list python-3.x indexing
2个回答
0
投票
#Set up variables

alpha = ['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']

morse = ['.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..']

#Get input from user

phrase = input('Enter a sentence to be converted to Morse Code')
output=""
for i in phrase:
    output += morse[alpha.index(i)]

-1
投票

我认为你应该自己想出一个解决方案,因为这不是一个真正的问题,你可以通过多种方式做到这一点,其中之一就是做
我认为你应该自己想出一个解决方案,因为这不是一个困难的问题,你可以通过多种方式做到这一点,其中之一就是做

For i in len(alpha):
phrase = phrase.replace(alpha[i], morse[i])

© www.soinside.com 2019 - 2024. All rights reserved.