我正在尝试编写一个使用索引将文本转换为莫尔斯电码的程序。我不会使用字典。到目前为止我的代码看起来像这样:
#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')
#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)]
我认为你应该自己想出一个解决方案,因为这不是一个真正的问题,你可以通过多种方式做到这一点,其中之一就是做
我认为你应该自己想出一个解决方案,因为这不是一个困难的问题,你可以通过多种方式做到这一点,其中之一就是做
For i in len(alpha):
phrase = phrase.replace(alpha[i], morse[i])