Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试着把摩尔斯电码翻译成英语_Python_Loops_Dictionary_If Statement_Morse Code - Fatal编程技术网

Python 试着把摩尔斯电码翻译成英语

Python 试着把摩尔斯电码翻译成英语,python,loops,dictionary,if-statement,morse-code,Python,Loops,Dictionary,If Statement,Morse Code,我正在用我用的字典把摩尔斯电码翻译成英语。我试图让它在用户输入信息的地方,它会将他/她的信息打印成莫尔斯电码 我发现翻译成摩尔斯电码很容易,然而把摩尔斯电码翻译成英语给了我一些问题 首先,如果我为'A'键入'.-'我实际上得到的是'E',因为它读取的是第一个'.'键,并将其转换为'E'而不是整个字符串 这就是我迄今为止所尝试的:) 这是我的字典 morse_eng_dict = {".-": "A", "-...": "B", "-.-.": "C", "-..": "D", ".": "E",

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

我发现翻译成摩尔斯电码很容易,然而把摩尔斯电码翻译成英语给了我一些问题

首先,如果我为'A'键入'.-'我实际上得到的是'E',因为它读取的是第一个'.'键,并将其转换为'E'而不是整个字符串

这就是我迄今为止所尝试的:)

这是我的字典

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。如果不借助大词典或模糊逻辑,就无法轻松区分它们

运算符倾向于使用空格分隔字母和单词,但是:


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

类似的方法应该会奏效: 基本上,当有空格时,它会分割字符串,生成一个列表,其中每个项目都是摩尔斯电码字母。然后,它根据字典检查每个字母,并取对应的英文字母。最后,它将所有这些内容放入一个列表中,再次将其转换为字符串并打印出来。 希望有帮助

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'}
字典['.-']会给你“a” 还有dictionary.get(“.-”)也会给你一个“a” 字典的索引是字符串,任何字符串,“.”与“.-”不同

","一","一","一","一",


顺便说一句,莫尔斯是一个二叉树,以E=left.node和T=right.node开头…

可能是重复的哦,非常感谢!对不起
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)