繁体   English   中英

如何让我的 Python 摩尔斯电码翻译器区分单点和破折号以及它们的序列?

[英]How would I make my Python Morse code translator distinguish between singular dots and dashes and a sequence of them?

我正在使用 CodeSkulptor (Python 2) 中的莫尔斯电码翻译器,并且有一个 function 用于在常规文本和莫尔斯电码之间进行翻译:

def input_handler(input_text):
    global inpu
    global input_message
    global output_message

    if inpu == "Text":
        input_message = input_text
        output_message = ''
        for character in input_text.lower():
            if character != ' ':
                output_message = output_message + morse_dict[character] + ' '
            else:
                output_message = output_message + '  ' 

但是,我无法让摩尔斯电码文本翻译工作。 它只输出 E 或 T,它们分别是一个点或破折号。 我相信这是因为我的 for 循环遍历单个字符,并且没有注册与字典中不同值匹配的点和破折号序列。 我也很难让 function 根据是否有不同的字母添加一个空格,或者当有不同的单词时添加两个空格。 这是摩尔斯电码和文本之间的翻译代码:

    elif inpu == "Morse Code":
        input_message = input_text
        output_message = ''
        for character in input_text:
            if character != ' ':
                output_message = output_message + alpha_dict[character] + ' '
            elif character == '  ':
                output_message = output_message + ' '

你的怀疑是正确的。

你需要考虑莫尔斯序列之间的空格,这意味着你不能在你有莫尔斯元素的情况下立即 output 一个文本字符,而必须等待一个完整的代码(多个信号,你不知道有多少事先)来在.

每次你读取一个空格(或用完输入),然后你检查你到目前为止得到了什么:

...  ---  ...

read ".", put it in buffer which is then "."
read ".", put it in buffer which is then ".."
read ".", put it in buffer which is then "..."
read " ", so check buffer; it is "...", so a "S". Empty the buffer.
read " ", so check buffer; it is empty, so do nothing
read "-", put it in buffer which is then "-"
...
nothing more to read, so check buffer; it is "...", so a "S".

...你会得到“SOS”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM