试图将摩尔斯电码翻译成英文
Trying to translate Morse code into English
我正在尝试用我使用的字典将摩尔斯电码翻译成英语。我试图在用户输入消息的地方使用它,它将 his/her 消息打印成摩尔斯电码。
我发现翻译成摩尔斯电码很容易,但是将摩尔斯电码翻译成英语给我带来了一些问题。
首先,如果我为 'A' 输入“.-”,我实际上得到的是 'E',因为它正在读取第一个“.”。键并将其翻译成 'E' 而不是获取整个字符串。
这是我迄今为止尝试过的方法:)
#If user types 'N' or 'n' it proceeds this loop
if morse_message == 'N' or morse_message == 'n': #Loops looks to see if morse_message was 'N' or 'n'
nomorse = input("Enter your Morse code here:")
nomorse_list = (nomorse.join('')
for letter in nomorse_list:
not_morse = (morse_eng_dict[letter.upper()])
print(not_morse)
这是我的字典
morse_eng_dict = {".-": "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"}
我想让 Python 获取我的摩尔斯电码并将消息打印回英文。比如像... .-.. .-.. ---(这是你好)
这是一个项目,所以我不能使用任何花哨的模块或任何东西。
有什么想法或意见吗?谢谢!
摩尔斯电码是用歧义构建的。正如您所指出的。-可以被视为 e t 或 a。如果不求助于大词典或一些模糊逻辑,就无法轻松区分它们。
operator 倾向于使用 spaces 来分隔字母和单词:
https://en.wikipedia.org/wiki/Morse_code
"The letters of a word are separated by a space equal to three dots (one dash), and the words are separated by a space equal to seven dots." 如果您插入 spaces,例如:在字母之间插入 1 space,在单词之间插入 2 spaces,到您的莫尔斯电码字符串中使解码变得小菜一碟(拆分然后映射)
像这样的东西应该可以工作:
基本上,只要有 space,它就会拆分字符串,生成一个列表,其中每个项目都是莫尔斯电码字母。然后它会根据字典检查每个字母并获取对应的英文字母。最后,它将所有这些放入一个列表中,再次将其转换为字符串并打印出来。
希望对您有所帮助!
morse_eng_dict = {".-": "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"}
nomorse = input("Enter your Morse code here:")
nomorse_list = nomorse.split() #this splits the string up wherever there is a space
not_morse = []
morse = True #The code is morse (so far)
for letter in nomorse_list:
eng_letter = False
for key in morse_eng_dict.keys(): #for each of the morse code letters in the dictionary
if letter == key:
eng_letter = morse_eng_dict[key]
if eng_letter: #if a letter was found that corresponds
not_morse.append(eng_letter)
else:
print("Input is not valid morse code.")
morse = False
if morse == True:
string = "".join(not_morse) #joining the string together (without spaces in between)
print(string)
对于任何 dictionary = {'.-': 'a'}
dictionary['.-'] 会给你 'a'
dictionary.get('.-') 也会给你一个 'a'
字典的索引是字符串、任何字符串和 '.'不同于'.-'
'.','.-','...','-.--'
btw morse 是一个以 E = left.node 和 T = right.node ...
开头的二叉树
我正在尝试用我使用的字典将摩尔斯电码翻译成英语。我试图在用户输入消息的地方使用它,它将 his/her 消息打印成摩尔斯电码。
我发现翻译成摩尔斯电码很容易,但是将摩尔斯电码翻译成英语给我带来了一些问题。
首先,如果我为 'A' 输入“.-”,我实际上得到的是 'E',因为它正在读取第一个“.”。键并将其翻译成 'E' 而不是获取整个字符串。
这是我迄今为止尝试过的方法:)
#If user types 'N' or 'n' it proceeds this loop
if morse_message == 'N' or morse_message == 'n': #Loops looks to see if morse_message was 'N' or 'n'
nomorse = input("Enter your Morse code here:")
nomorse_list = (nomorse.join('')
for letter in nomorse_list:
not_morse = (morse_eng_dict[letter.upper()])
print(not_morse)
这是我的字典
morse_eng_dict = {".-": "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"}
我想让 Python 获取我的摩尔斯电码并将消息打印回英文。比如像... .-.. .-.. ---(这是你好)
这是一个项目,所以我不能使用任何花哨的模块或任何东西。 有什么想法或意见吗?谢谢!
摩尔斯电码是用歧义构建的。正如您所指出的。-可以被视为 e t 或 a。如果不求助于大词典或一些模糊逻辑,就无法轻松区分它们。
operator 倾向于使用 spaces 来分隔字母和单词: https://en.wikipedia.org/wiki/Morse_code
"The letters of a word are separated by a space equal to three dots (one dash), and the words are separated by a space equal to seven dots." 如果您插入 spaces,例如:在字母之间插入 1 space,在单词之间插入 2 spaces,到您的莫尔斯电码字符串中使解码变得小菜一碟(拆分然后映射)
像这样的东西应该可以工作: 基本上,只要有 space,它就会拆分字符串,生成一个列表,其中每个项目都是莫尔斯电码字母。然后它会根据字典检查每个字母并获取对应的英文字母。最后,它将所有这些放入一个列表中,再次将其转换为字符串并打印出来。 希望对您有所帮助!
morse_eng_dict = {".-": "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"}
nomorse = input("Enter your Morse code here:")
nomorse_list = nomorse.split() #this splits the string up wherever there is a space
not_morse = []
morse = True #The code is morse (so far)
for letter in nomorse_list:
eng_letter = False
for key in morse_eng_dict.keys(): #for each of the morse code letters in the dictionary
if letter == key:
eng_letter = morse_eng_dict[key]
if eng_letter: #if a letter was found that corresponds
not_morse.append(eng_letter)
else:
print("Input is not valid morse code.")
morse = False
if morse == True:
string = "".join(not_morse) #joining the string together (without spaces in between)
print(string)
对于任何 dictionary = {'.-': 'a'}
dictionary['.-'] 会给你 'a'
dictionary.get('.-') 也会给你一个 'a'
字典的索引是字符串、任何字符串和 '.'不同于'.-'
'.','.-','...','-.--'
btw morse 是一个以 E = left.node 和 T = right.node ...
开头的二叉树