2017-02-21 162 views
2

我在完成此代码时遇到问题。我最初的计划是接受一个长字符串作为输入,例如'... --- .. - 。 - .. .... ...-。'然后能够使用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() 

回答

2

这应该为你工作:

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(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'} 

要使用字典:

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

这看起来很干净。谢谢你的建议! – ccbrantley

+0

@ccbrantley欢迎您! –

+0

@ccbrantley请记住接受答案! –

4

你的主意,用分裂应该只是罚款:

>>> '... ---.. -. -.. .... . .-.. .--.'.split() 
['...', '---..', '-.', '-..', '....', '.', '.-..', '.--.'] 

对于翻译,我会用,而不是一个列表的字典:

morse2text = {'...': 's', '---': 'o'} 

为了避免关键错误,我会做一个“安全查找”使用字典get()方法:

print(morse2text.get(m, m)) 

这里是情况放在一起(尽管不完全字典)的所有代码,你想建立过一个工作实例:

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

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

而.split()的作品分割字符串除了有似乎是,当因为没有返回值,它是通过跑了问题? – ccbrantley

+0

从来没有... morse_code = code_input.split()的作品。这不起作用,morse_code = [code_input.split()]。我一直试图弥补这个小时谢谢你! – ccbrantley