莫尔斯到英语程序函数调用



这是一个将莫尔斯电码转换为英语文本的程序。

我很难把莫尔斯电码转换成英语。我得到的输出只有星号。我有另一个函数,可以从字符串转换为莫尔斯电码。decodeMorsetoChar(text_input)函数似乎没有将转换后的莫尔斯电码转换回英语。

def encodeStringtoMorse(text_input):

length=len(text_input)
string=''
for x in range(0, length):
char= encodeChartoMorse(text_input[x])
string=string + '    ' + char
return string 

def decodeMorsetoChar(text_input):

morse=encodeStringtoMorse(text_input.upper())
if morse== ".-" :
morse = "A"
elif morse== "-..." :
morse = "B"
elif morse== "-.-." :
morse = "C"
elif morse== "-.." :
morse = "D"
elif morse== ".":
morse = "E"
elif morse== "..-." :
morse = "F"
elif morse== "--.":
morse = "G"
elif morse== "...." :
morse = "H"
elif morse== ".." :
morse = "I"
elif morse== ".---" :
morse = "J"
elif morse== "-.-":
morse = "K"
elif morse== ".-..":
morse = "L"
elif morse== "--" :
morse = "M"
elif morse== "-.":
morse = "N"
elif morse== "---":
morse = "O"
elif morse== ".--.":
morse = "P"
elif morse== "--.-":
morse = "Q"
elif morse==".-.":
morse = "R"
elif morse== "..." :
morse = "S"
elif morse== "-":
morse = "T"
elif morse== "..-":
morse = "U"
elif morse== "...-":
morse = "V"
elif morse== ".--" :
morse = "W"
elif morse== "-..-" :
morse = "X"
elif morse== "-.--":
morse = "Y"
elif morse== "--.." :
morse = "Z"
else:
morse= '*'


return morse

def decodeMorsetoString(text_input):

listt=''
morsecode=decodeMorsetoChar(text_input)
for morse in text_input.split():
morsecode=decodeMorsetoChar(text_input)
listt= listt+morsecode

return listt

以下是我对Jalen答案的重构:

morse_alphabet = {
'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': "--..",
}
alphabet_morse = {v: k for k, v in morse_alphabet.items()}

def string_to_morse(text):
return ''.join([morse_alphabet.get(c.upper(), '*') for c in text])

def morse_to_string(morse):
return ''.join([alphabet_morse[m] for m in morse.split('*')])

string_to_morse("Hello world")
morse_to_string("-..-*--..")

试试这个:

morseAlphabet = {
'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': "--..",
'Default': "*"
}

def decodeMorsetoChar(text_input):
list_chars = list(text_input)
morse_string = ''
for char in list_chars:
try:
morse_string += morseAlphabet[char.upper()]
except:
morse_string += "*"
return morse_string

def decodeMorsetoString(morse_input):
list_morse = list(morse_input.split('*'))
string_from_morse = ''
# for morse in list_morse:
# for key in morseAlphabet:
#     if morseAlphabet[key] == morse:
#         string_from_morse += key
alphabet_morse = {v: k for k, v in morseAlphabet.items()}
for code in list_morse:
string_from_morse += alphabet_morse[code]
return string_from_morse

decodeMorsetoChar("Hello world")
decodeMorsetoString("-..-*--..")

最新更新