`I'm trying to write code that starts with a question asking the user if they want to encode or decode to/from morse. Based on their response (1 or 2), it runs through an if statement, and will call the required function(s).
It will take a user's input via user_input() and either return it in morse code, or return it in English, based on their choice to encode or decode. The encoding aspect works, but I cannot get the decode_morse() function to work within the entire program.
I'm getting an error on the calling of function encode_or_decode() at the bottom, and also '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()
I've tried running a similar decode function in isolation with its own user input and this works fine... but I don't want to duplicate the user input function in the main program, so have tried to use the data variable from the user_input(_) function, which throws up errors.
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))
CodePudding user response:
The function decode_morse()
is defined twice: one with a parameter, and one without. Try to change the names of the functions.
CodePudding user response:
I managed to figure it out - I needed to print the results of the decode_morse() function and remove 'return results.lower()' as this stopped the print from executing. The correct code was:
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()