Advertisement
rex9840

Untitled

Nov 13th, 2021
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1.  
  2. #Save New Duplicate & Edit Just Text Twitter
  3. # Defining Morse Code Key
  4. stop = False
  5. morseOut=' '
  6. morseKey = (('a','.-'),('b','-...'),('c','-.-.'),('d','-..'),('e','.'),
  7.  
  8.       ('f','..-.'),('g','--.'),('h','....'),('i','..'),('j','.---'),
  9.  
  10.       ('k','-.-'),('l','.-..'),('m','--'),('o','---'),('p','.--.'),
  11.  
  12.       ('q','--.-'),('r','.-.'),('s','...'),('t','-'),('u','..-'),
  13.  
  14.       ('v','...-'),('w','.--'),('x','-..-'),('y','-.--'),('z','--..'))
  15.  
  16. # This is where the program will greet the user
  17.  
  18. print('Welcome!')
  19. print('This program was designed to translate English to Morse Code, and Morse Code to English.')
  20.  
  21.  
  22.  
  23. # This is where the program prompts the user, asking if they want Morse Code or English translated
  24.  
  25.  
  26.  
  27. while stop == False:
  28.   which = input (' Would you like to translate from Morse to English (M) or English to Morse Code (E) : ')
  29.  
  30.  
  31.  
  32.   while which != 'm' and which != 'e':
  33.     which = input ('Incorrect Option! Please enter either - (m) or (e) only.')
  34. #   translatingM = (which == 'M')
  35.   unitIn = input('Please enter a letter, word, or phrase that you would like translated : ')
  36.  
  37. # Input Translations
  38.  
  39. #   if translatingM == True:
  40.   if which in "Mm":
  41.     unitIn = unitIn.lower();
  42.     fromIndex = 0
  43.     toIndex = 1
  44.  
  45.   else:
  46.  
  47.     unitIn = unitIn.split()
  48.     fromIndex = 1
  49.     toIndex = 0
  50.  
  51.   for character in unitIn:
  52.     letterFound = False
  53.  
  54.     if which in "Mm":
  55.  
  56.       for mainIndex in morseKey:
  57.         if character == mainIndex [fromIndex]:
  58.           morseOut = morseOut + ' ' + mainIndex [toIndex]
  59.           letterFound = True
  60.  
  61.     else:
  62.         for mainIndex in morseKey:
  63.             if character == mainIndex [fromIndex]:
  64.                 morseOut = morseOut + mainIndex [toIndex]
  65.                 letterFound = True
  66.  
  67.   print(morseOut)
  68.   # Reset
  69.  
  70.   translationOut = ' '
  71.  
  72.   # The Program will ask the user if another is needed
  73.  
  74.  
  75.   response = input ('Do you need another translation done? (y or n): ')
  76.  
  77.   while response != 'y' and response != 'n':
  78.     response = input ('Please enter either (y) or (n): ')
  79.     response = response.lower();
  80.  
  81.   if response == 'n' :
  82.     print('Thank you! Have a great day!')
  83.     stop = True
  84.  
  85.   else:
  86.  
  87.     print(' Not a real command. Please Enter either  - (m) or (e) only.')
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement