-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