asweigart

Untitled

Dec 11th, 2024
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. # English to pig latin
  2. print('Enter the English message to translate into pig latin:')
  3. message = input()
  4.  
  5. VOWELS = ('a', 'e', 'i', 'o', 'u', 'y')
  6.  
  7. pig_latin = [] # A list of the words in pig latin
  8. for word in message.split():
  9. # Separate the non-letters at the start of this word:
  10. prefix_non_letters = ''
  11. while len(word) > 0 and not word[0].isalpha():
  12. prefix_non_letters += word[0]
  13. word = word[1:]
  14. if len(word) == 0:
  15. pig_latin.append(prefix_non_letters)
  16. continue
  17.  
  18. # Separate the non-letters at the end of this word:
  19. suffix_non_letters = ''
  20. while not word[-1].isalpha():
  21. suffix_non_letters += word[-1] + suffix_non_letters
  22. word = word[:-1]
  23.  
  24. # Remember if the word was in uppercase or title case:
  25. was_upper = word.isupper()
  26. was_title = word.istitle()
  27.  
  28. word = word.lower() # Make the word lowercase for translation.
  29.  
  30. # Separate the consonants at the start of this word:
  31. prefix_consonants = ''
  32. while len(word) > 0 and not word[0] in VOWELS:
  33. prefix_consonants += word[0]
  34. word = word[1:]
  35.  
  36. # Add the pig latin ending to the word:
  37. if prefix_consonants != '':
  38. word += prefix_consonants + 'ay'
  39. else:
  40. word += 'yay'
  41.  
  42. # Set the word back to uppercase or title case:
  43. if was_upper:
  44. word = word.upper()
  45. if was_title:
  46. word = word.title()
  47.  
  48. # Add the non-letters back to the start or end of the word.
  49. pig_latin.append(prefix_non_letters + word + suffix_non_letters)
  50.  
  51. # Join all the words back together into a single string:
  52. print(' '.join(pig_latin))
  53.  
Advertisement
Add Comment
Please, Sign In to add comment