Advertisement
rex9840

Untitled

Nov 13th, 2021
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1.  
  2. #Save New Duplicate & Edit Just Text Twitter
  3. # Defining Morse Code Key
  4. stop = False
  5. morseKey = (('a','.-'),('b','-...'),('c','-.-.'),('d','-..'),('e','.'),
  6.  
  7.       ('f','..-.'),('g','--.'),('h','....'),('i','..'),('j','.---'),
  8.  
  9.       ('k','-.-'),('l','.-..'),('m','--'),('o','---'),('p','.--.'),
  10.  
  11.       ('q','--.-'),('r','.-.'),('s','...'),('t','-'),('u','..-'),
  12.  
  13.       ('v','...-'),('w','.--'),('x','-..-'),('y','-.--'),('z','--..'))
  14.  
  15. # This is where the program will greet the user
  16.  
  17. print('Welcome!')
  18. print('This program was designed to translate English to Morse Code, and Morse Code to English.')
  19.  
  20.  
  21.  
  22. # This is where the program prompts the user, asking if they want Morse Code or English translated
  23.  
  24.  
  25.  
  26. while stop == False:
  27.   morseOut=' '
  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 "Ee":
  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.    
  53.     letterFound = False
  54.  
  55.     if which in "Ee":
  56.  
  57.       for mainIndex in morseKey:
  58.         if character == mainIndex [fromIndex]:
  59.           morseOut = morseOut + ' ' + mainIndex [toIndex]
  60.           letterFound = True
  61.  
  62.     else:
  63.         for mainIndex in morseKey:
  64.             if character == mainIndex [fromIndex]:
  65.                 morseOut = morseOut + mainIndex [toIndex]
  66.                 letterFound = True
  67.  
  68.   print(morseOut)
  69.   # Reset
  70.  
  71.   translationOut = ' '
  72.  
  73.   # The Program will ask the user if another is needed
  74.  
  75.  
  76.   response = input ('Do you need another translation done? (y or n): ')
  77.  
  78.   while response != 'y' and response != 'n':
  79.     response = input ('Please enter either (y) or (n): ')
  80.     response = response.lower();
  81.  
  82.   if response == 'n' :
  83.     print('Thank you! Have a great day!')
  84.     stop = True
  85.  
  86.   else:
  87.  
  88.     print(' Not a real command. Please Enter either  - (m) or (e) only.')
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement