繁体   English   中英

摩尔斯电码转换器 - 在 if 语句中使用 Python 调用 function

[英]Morse Code Translator - Calling a function within an if statement using Python

`我正在尝试编写以询问用户是否要编码或解码莫尔斯码的问题开头的代码。 根据他们的响应(1 或 2),它会运行一个 if 语句,并将调用所需的函数。

它将通过 user_input() 获取用户的输入,并根据用户选择编码或解码,以摩尔斯电码返回,或以英语返回。 编码方面有效,但我无法让 decode_morse() function 在整个程序中工作。

我在底部调用 function encode_or_decode() 时遇到错误,还有“TypeError: decode_morse() missing 1 required positional argument: 'data'”


# 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()

我试过运行一个类似的解码 function 与它自己的用户输入隔离,这工作正常......但我不想在主程序中复制用户输入 function,所以尝试使用数据变量user_input(_) function,它会抛出错误。

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))

function decode_morse()定义了两次:一次有参数,一次没有。 尝试更改函数的名称。

我设法弄清楚了 - 我需要打印 decode_morse() function 的结果并删除“return results.lower()”,因为这会停止执行打印。 正确的代码是:

def decode_morse(data):
    results = []
    for item in data.split(' '):
        results.append(MORSE_TO_ENG_DICT.get(item))
    results = ''.join(results)
    print(results.lower())
encode_or_decode()

暂无
暂无

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

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