|
| 1 | +# Morse Code Dictionary |
| 2 | +MORSE_CODE_DICT = { |
| 3 | + 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....', |
| 4 | + 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', |
| 5 | + 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', |
| 6 | + 'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', |
| 7 | + '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----', ',': '--..--', '.': '.-.-.-', |
| 8 | + '?': '..--..', '/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-', ' ': '/' |
| 9 | +} |
| 10 | + |
| 11 | +# Function to encode a message to Morse code |
| 12 | +def encode_to_morse(message): |
| 13 | + try: |
| 14 | + morse_code = '' |
| 15 | + for char in message.upper(): |
| 16 | + if char not in MORSE_CODE_DICT: |
| 17 | + raise ValueError(f"Character '{char}' cannot be encoded into Morse code.") |
| 18 | + morse_code += MORSE_CODE_DICT[char] + ' ' |
| 19 | + return morse_code.strip() |
| 20 | + except ValueError as ve: |
| 21 | + print(ve) |
| 22 | + return None |
| 23 | + except Exception as e: |
| 24 | + print(f"An unexpected error occurred: {e}") |
| 25 | + return None |
| 26 | + |
| 27 | +# Function to decode Morse code to a message |
| 28 | +def decode_from_morse(morse_message): |
| 29 | + try: |
| 30 | + morse_words = morse_message.split(' / ') # Words are separated by '/' |
| 31 | + decoded_message = '' |
| 32 | + for word in morse_words: |
| 33 | + for morse_char in word.split(): |
| 34 | + if morse_char not in MORSE_CODE_DICT.values(): |
| 35 | + raise ValueError(f"Morse code '{morse_char}' is invalid.") |
| 36 | + decoded_message += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT.values()).index(morse_char)] |
| 37 | + decoded_message += ' ' |
| 38 | + return decoded_message.strip() |
| 39 | + except ValueError as ve: |
| 40 | + print(ve) |
| 41 | + return None |
| 42 | + except Exception as e: |
| 43 | + print(f"An unexpected error occurred: {e}") |
| 44 | + return None |
| 45 | + |
| 46 | +# Main program loop |
| 47 | +if __name__ == "__main__": |
| 48 | + while True: |
| 49 | + print("\nMorse Code Encoder/Decoder") |
| 50 | + print("1. Encode a message to Morse code") |
| 51 | + print("2. Decode Morse code to a message") |
| 52 | + print("3. Exit") |
| 53 | + |
| 54 | + try: |
| 55 | + choice = int(input("Select an option (1/2/3): ")) |
| 56 | + if choice == 1: |
| 57 | + message = input("Enter a message to encode: ") |
| 58 | + result = encode_to_morse(message) |
| 59 | + if result: |
| 60 | + print(f"Encoded Morse Code: {result}") |
| 61 | + elif choice == 2: |
| 62 | + morse_message = input("Enter Morse code to decode (use '/' for space between words): ") |
| 63 | + result = decode_from_morse(morse_message) |
| 64 | + if result: |
| 65 | + print(f"Decoded Message: {result}") |
| 66 | + elif choice == 3: |
| 67 | + print("Goodbye!") |
| 68 | + break |
| 69 | + else: |
| 70 | + print("Invalid option. Please select 1, 2, or 3.") |
| 71 | + except ValueError: |
| 72 | + print("Please enter a valid number for selection.") |
| 73 | + except Exception as e: |
| 74 | + print(f"An unexpected error occurred: {e}") |
0 commit comments