'我正在编写一段代码,它以一个问题开始,询问用户是否要对莫尔斯进行编码或解码。根据他们的回答(1或2),它将运行一个if语句,并调用所需的函数。
它将通过user_input()接收用户的输入,并根据用户选择的编码或解码,返回莫尔斯电码或英语。编码方面可以工作,但我无法让decode_morse()函数在整个程序中工作。
调用底部的encode_or_decode()函数时出现错误,同时出现“TypeError:decode_莫尔斯()缺少1个必需的位置参数:'数据''
# Dictionary representing English to morse code chart
ENG_TO_MORSE_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':'--..', ' ':'/', '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':'--..'
}
# Dictionary representing morse code to English
MORSE_TO_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", "/":' '
}
def encode_or_decode():
choice = int(input("Please select 1 to encode to morse, or 2 to decode from morse "))
if choice == 1:
message_to_encode()
elif choice == 2:
decode_morse()
else:
print("Please select option 1 or option 2")
# Defining a global variable for user's input to be used within multiple functions
def user_input():
global data
data = str(input("What message do you want to translate using the Morse cipher? "))
def morse_encrypt(data):
for letter in data:
print(ENG_TO_MORSE_DICT[letter], end = ' ')
# Defining a function for user-inputted data, using isalpha method to mandate only alphabet letters & spaces as input
def message_to_encode():
user_input()
if data.replace(' ', '').isalpha():
morse_encrypt(data)
else:
print("Only text allowed in message")
def decode_morse():
results = []
for item in data.split(' '):
results.append(MORSE_TO_ENG_DICT.get(item))
results = ''.join(results)
return results.lower()
def decode_morse(data):
results = []
for item in data.split(' '):
results.append(MORSE_TO_ENG_DICT.get(item))
results = ''.join(results)
return results.lower()
encode_or_decode()
我试过单独运行一个类似的decode函数,它有自己的用户输入,运行得很好......但是我不想在主程序中复制用户输入函数,所以我试过使用user_input(_)函数中的data变量,这会引发错误。
MORSE_TO_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", "/":' '
}
def decode_morse(morse_data):
results = []
for item in morse_data.split(' '):
results.append(MORSE_TO_ENG_DICT.get(item))
results = ''.join(results)
return results.lower()
morse_data = str(input("What morse message do you want to decode using the Morse cipher? "))
print(decode_morse(morse_data))
2条答案
按热度按时间djmepvbi1#
函数
decode_morse()
定义了两次:一个带参数,一个不带参数。请尝试更改函数的名称。evrscar22#
我设法弄明白了-我需要打印decode_莫尔斯()函数的结果并删除'return results.lower()',因为这会阻止打印执行。正确的代码是: