Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- morse_code_dictionary = {
- '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': '-----',
- ', ': '--..--',
- '.': '.-.-.-',
- '?': '..--..',
- '/': '-..-.',
- '-': '-....-',
- '(': '-.--.',
- ')': '-.--.-'
- }
- def encrypt(message):
- """Encrypts a string to Morse code."""
- cipher = ''
- for letter in message.upper():
- if letter != ' ':
- cipher += morse_code_dictionary.get(letter, '#') + ' '
- else:
- cipher += ' / '
- return cipher
- def decrypt(message):
- """Decrypts a Morse code string to plain text."""
- message += ' '
- decipher = ''
- citext = ''
- for letter in message:
- if letter != ' ':
- citext += letter
- else:
- if citext in morse_code_dictionary.values():
- decipher += list(morse_code_dictionary.keys())[list(
- morse_code_dictionary.values()).index(citext)]
- citext = ''
- return decipher
- # Get user input for message and choice
- choice = input("Enter 'e' for encrypt, 'd' for decrypt: ")
- message = input("Enter message: ")
- if choice == 'e':
- result = encrypt(message)
- print(f"Encrypted message: {result}")
- elif choice == 'd':
- result = decrypt(message)
- print(f"Decrypted message: {result}")
- else:
- print("Invalid choice.")
Advertisement
Add Comment
Please, Sign In to add comment