Sunday, April 5, 2020

Python program for converting English to Morse and back ...

Morse code is a method used in telecommunication to encode text characters as sequences of dots and dashes. It is named after Samuel Morse, an inventor of the telegraph. 

  • Input your first name and check its MORSE form.
  • Input your surname in MORSE and check its English form.

Morse1 = {'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':'--..'
         }

fName = input("Enter your first name: ").upper()

Encrypted, Morse2 = '', {}

for i in fName:
  if i == ' ':
    Encrypted += "/ "
  else:
    Encrypted += Morse1[i]  + " "

print("\n", Encrypted, "\n")

Morse2 = {'.-':'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'
         }

lName = input("Enter your surname in Morse: ")
if lName[-1] != ' ':   # Prevent last character from being skipped
  lName += ' '
  
Decrypted, s = '', ''

for i in lName:
  if i != ' ':
    s += i
  else:
    if s[0] != ' ' and s[0] != '':
      Decrypted += Morse2[s]
    s = ''

print(Decrypted)

No comments:

Post a Comment