【问题标题】:Trying to translate Morse code into English试图将摩尔斯电码翻译成英文
【发布时间】:2023-03-31 18:23:01
【问题描述】:

我正在尝试使用我使用的字典将摩尔斯电码翻译成英语。我试图让它在用户输入消息的地方,它将他/她的消息打印成摩尔斯电码。

我发现翻译成摩尔斯电码很容易,但是将摩尔斯电码翻译成英语给我带来了一些问题。

首先,如果我为 '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 获取我的摩尔斯电码并将消息打印回英文。比如...之类的东西.-.. .-.. --- (HELLO)

这是一个项目,所以我不能使用任何花哨的模块或任何东西。 有什么想法或意见吗?谢谢!

【问题讨论】:

标签: python loops dictionary if-statement morse-code


【解决方案1】:

这样的事情应该可以工作: 基本上,只要有空格,它就会拆分字符串,制作一个列表,其中每个项目都是一个莫尔斯电码字母。然后它根据字典检查每个字母并获取英文对应项。最后,它将所有这些放入一个列表中,再次将其转换为字符串并打印出来。 希望对您有所帮助!

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)

【讨论】:

    【解决方案2】:

    莫尔斯电码是模棱两可的。正如您指出的那样.- 可以被视为 e t 或 a。如果不借助大字典或一些模糊逻辑,就无法轻松区分它们。

    运算符倾向于使用空格来分隔字母和单词,但是: https://en.wikipedia.org/wiki/Morse_code

    “单词的字母之间用一个等于三个点(一个破折号)的空格隔开,单词之间用一个等于七个点的空格隔开。”如果您在莫尔斯电码字符串中插入空格,例如:字母之间有 1 个空格,单词之间有 2 个空格,这将使解码变得轻而易举(先拆分然后映射)

    【讨论】:

      【解决方案3】:

      对于任何dictionary = {'.-': 'a'} dictionary['.-'] 会给你'a' dictionary.get('.-') 也会给你一个'a' 字典的索引是字符串、任何字符串和 '.'不同于'.-'

      '.','.-','...','-.--'

      btw morse 是一棵以 E = left.node 和 T = right.node 开头的二叉树 ...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多