Guest User

Untitled

a guest
Oct 17th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. morse_code_dictionary = {
  2. 'A': '.-',
  3. 'B': '-...',
  4. 'C': '-.-.',
  5. 'D': '-..',
  6. 'E': '.',
  7. 'F': '..-.',
  8. 'G': '--.',
  9. 'H': '....',
  10. 'I': '..',
  11. 'J': '.---',
  12. 'K': '-.-',
  13. 'L': '.-..',
  14. 'M': '--',
  15. 'N': '-.',
  16. 'O': '---',
  17. 'P': '.--.',
  18. 'Q': '--.-',
  19. 'R': '.-.',
  20. 'S': '...',
  21. 'T': '-',
  22. 'U': '..-',
  23. 'V': '...-',
  24. 'W': '.--',
  25. 'X': '-..-',
  26. 'Y': '-.--',
  27. 'Z': '--..',
  28. '1': '.----',
  29. '2': '..---',
  30. '3': '...--',
  31. '4': '....-',
  32. '5': '.....',
  33. '6': '-....',
  34. '7': '--...',
  35. '8': '---..',
  36. '9': '----.',
  37. '0': '-----',
  38. ', ': '--..--',
  39. '.': '.-.-.-',
  40. '?': '..--..',
  41. '/': '-..-.',
  42. '-': '-....-',
  43. '(': '-.--.',
  44. ')': '-.--.-'
  45. }
  46.  
  47.  
  48. def encrypt(message):
  49. """Encrypts a string to Morse code."""
  50. cipher = ''
  51. for letter in message.upper():
  52. if letter != ' ':
  53. cipher += morse_code_dictionary.get(letter, '#') + ' '
  54. else:
  55. cipher += ' / '
  56. return cipher
  57.  
  58.  
  59. def decrypt(message):
  60. """Decrypts a Morse code string to plain text."""
  61. message += ' '
  62. decipher = ''
  63. citext = ''
  64. for letter in message:
  65. if letter != ' ':
  66. citext += letter
  67. else:
  68. if citext in morse_code_dictionary.values():
  69. decipher += list(morse_code_dictionary.keys())[list(
  70. morse_code_dictionary.values()).index(citext)]
  71. citext = ''
  72. return decipher
  73.  
  74.  
  75. # Get user input for message and choice
  76.  
  77. choice = input("Enter 'e' for encrypt, 'd' for decrypt: ")
  78. message = input("Enter message: ")
  79.  
  80. if choice == 'e':
  81. result = encrypt(message)
  82. print(f"Encrypted message: {result}")
  83. elif choice == 'd':
  84. result = decrypt(message)
  85. print(f"Decrypted message: {result}")
  86. else:
  87. print("Invalid choice.")
  88.  
Advertisement
Add Comment
Please, Sign In to add comment