below the code works message to mores and mores to message. but I want to make this mores code converter as a website using flask what the form(wtf). but I have no idea how to do it. if you can code it I would really appreciate it.
from flask import Flask
MORSE_CODE_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':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-', ' ': ' '
}
app = Flask(__name__)
def morse_code_converter(input_message, cipher_direction):
if cipher_direction == "encode":
my_cipher = [MORSE_CODE_DICT[char] for char in input_message]
return " ".join(my_cipher)
else:
my_message = input_message.split()
decipher = [key for char in my_message for key, value in MORSE_CODE_DICT.items() if value == char]
return " ".join(decipher)
def main():
should_end = False
while not should_end:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt: ").lower()
if direction == "encode" or direction == "decode":
message = input("Type your message: ").upper()
output = morse_code_converter(input_message=message, cipher_direction=direction)
if direction == "encode":
print(f"your message to morse code is {output}")
else:
print(f"your morse code to message is {output}")
restart = input("Type 'yes' if you want to go again. Otherwise type 'no': ").lower()
if restart == "no":
should_end = True
print("Goodbye")
else:
print("You type it wrong. Type 'encode' or 'decode'")
if __name__ == '__main__':
main()