-1
我需要编写一个程序,可以使用莫尔斯码对消息进行编码/解码。摩尔斯电码到字母数字(python)
要对消息进行编码,程序需要读取包含字母数字字符的输入文件,然后输出相应的莫尔斯编码消息。
要解码消息,程序将读取带莫尔斯码的文件,然后输出字母数字字符。
这是我迄今为止,但我不能让它正确地解码:
import string
def to_from(text, dict_in):
for i in text:
if i.upper() in dict_in:
print "%6s" % (dict_in[i]),
else:
print i, "not in dictionary"
print
letters = {'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': '--**',
'0': '-----', '1': '*----', '2': '**---',
'3': '***--', '4': '****-', '5': '*****',
'6': '-****', '7': '--***', '8': '---**',
'9': '----*'
}
frommorse = dict((b,a) for a,b in letters.items())
text = raw_input('What is the translation you need?\n')
text=text.strip()
if text[0] in string.letters:
to_from(text, letters)
else:
to_from(text, frommorse)
当我试图解码* - ,它会打印出“ET”,而不是A.我在做什么错误?
提示:使用字典来代替。 – Zero
该网站的目的是为了编程问题,而不是期待他人做你的工作。请告诉我们你已经尝试了什么,以及你被困住了什么。 – ByteHamster
贝蒂:不要理会@ ByteHamster的评论,懒虫们一直来这里,让别人为他们做工作,在这个过程中一点也不知道什么。 – martineau