我很难完成这段代码。我最初的计划是接受一个长字符串作为输入,例如‘.-.-。-..……。-“然后可以使用morse_code = code_input.split(‘')来分离它们并单独运行它们,但是我要么从索引中返回第一个字符,要么根本没有返回,所以有人能帮我修复这个问题或者帮助引导我找到一个更简单的解决方案吗?感谢大家的帮助!
#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()发布于 2017-02-21 04:27:53
这应该适用于你:
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'}使用dict:
translation = ''
for c in morse_codes:
translation += d[c] 发布于 2017-02-21 04:27:03
您使用拆分的想法应该运行得很好:
>>> '... ---.. -. -.. .... . .-.. .--.'.split()
['...', '---..', '-.', '-..', '....', '.', '.-..', '.--.']对于翻译,我会使用字典而不是列表:
morse2text = {'...': 's', '---': 'o'}为了避免关键错误,我将使用字典https://docs.python.org/3/library/stdtypes.html#dict.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) )https://stackoverflow.com/questions/42358657
复制相似问题