Hi Bartleby, by using this code : def print_intro(): print("Welcome to Wolmorse") print("This program encodes and decodes Morse code.") def get_input(): cont = True while cont: ch1 = input("Would you like to encode (e) or decode (d): ") if ch1 in 'de': ch2 = input("Would you like to read from a file (f) or console (c): ") if ch1 == 'd' and ch2 == 'c': msg = input("What message would you like to decode: ") print(decode(msg)) elif ch1 == 'e' and ch2 == 'c': msg = input("What message would you like to encode: ") print(encode(msg)) elif ch1 == 'd' and ch2 == 'f': file_name = input("Enter a filename: ") if check_file_exists(file_name): msg = process_lines(file_name, 'r') result = decode(msg) write_lines(result) else: print("Invalid Filename") elif ch1 == 'e' and ch2 == 'f': file_name = input("Enter a filename: ") if check_file_exists(file_name): msg = process_lines(file_name, 'r') result = encode(msg) write_lines(result) else: print("Invalid Filename") ch_cont = input("Would you like to encode/decode another message: ") if ch_cont == 'n': cont = False print("Thanks for using the program, Goodbye. ") def encode(message): encode_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': '--..', ' ': '.....'} encoded = ' '.join(encode_dict[i] for i in message.upper()) return encoded def decode(message): encode_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': '--..', ' ': '.....'} decode_dict = {v: k for k, v in encode_dict.items()} decoded = ''.join(decode_dict[i] for i in message.split()) return decoded def process_lines(filename, mode): with open(filename, 'r') as f: msg = f.read() return msg def write_lines(lines): with open('results.txt', 'w') as f: f.writelines(lines) print("Output written to results.txt") def check_file_exists(filename): from os.path import exists file_exists = exists(filename) return file_exists def main(): print_intro() get_input() # Call main function main() I need to furthermore to continue this and I need some help understanding it,
Hi Bartleby, by using this code :
def print_intro():
print("Welcome to Wolmorse")
print("This program encodes and decodes Morse code.")
def get_input():
cont = True
while cont:
ch1 = input("Would you like to encode (e) or decode (d): ")
if ch1 in 'de':
ch2 = input("Would you like to read from a file (f) or console (c): ")
if ch1 == 'd' and ch2 == 'c':
msg = input("What message would you like to decode: ")
print(decode(msg))
elif ch1 == 'e' and ch2 == 'c':
msg = input("What message would you like to encode: ")
print(encode(msg))
elif ch1 == 'd' and ch2 == 'f':
file_name = input("Enter a filename: ")
if check_file_exists(file_name):
msg = process_lines(file_name, 'r')
result = decode(msg)
write_lines(result)
else:
print("Invalid Filename")
elif ch1 == 'e' and ch2 == 'f':
file_name = input("Enter a filename: ")
if check_file_exists(file_name):
msg = process_lines(file_name, 'r')
result = encode(msg)
write_lines(result)
else:
print("Invalid Filename")
ch_cont = input("Would you like to encode/decode another message: ")
if ch_cont == 'n':
cont = False
print("Thanks for using the program, Goodbye. ")
def encode(message):
encode_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': '--..', ' ': '.....'}
encoded = ' '.join(encode_dict[i] for i in message.upper())
return encoded
def decode(message):
encode_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': '--..', ' ': '.....'}
decode_dict = {v: k for k, v in encode_dict.items()}
decoded = ''.join(decode_dict[i] for i in message.split())
return decoded
def process_lines(filename, mode):
with open(filename, 'r') as f:
msg = f.read()
return msg
def write_lines(lines):
with open('results.txt', 'w') as f:
f.writelines(lines)
print("Output written to results.txt")
def check_file_exists(filename):
from os.path import exists
file_exists = exists(filename)
return file_exists
def main():
print_intro()
get_input()
# Call main function
main()
I need to furthermore to continue this and I need some help understanding it, thank you


Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 6 images









