Pig Latin is a word game that transforms English words into a parody of Latin. In Pig Latin, if a word begins with a consonant, the speaker removes this letter and puts it at the end, followed by “ay.” For example, “pig” becomes “igpay” and “latin” becomes “atinlay.” Otherwise, if the word begins with a vowel, the speaker simply adds “yay” to the end of it. For example, “elephant” becomes “elephantyay” and “umbrella” becomes “umbrellayay.”
When you run piglatin.py, the output will look like this:
Igpay Atinlay (Pig Latin)
By Al Sweigart [email protected]
Enter your message:
> This is a very serious message.
Isthay isyay ayay eryvay erioussay essagemay.
(Copied pig latin to clipboard.)
The englishToPigLatin()
function takes a string of English text and returns a string of its Pig Latin equivalent. The main()
function only gets called if a user runs the program directly. You could also write your own Python programs, import piglatin.py with an import piglatin
statement, then call piglatin.englishToPigLatin()
to make use of the englishToPigLatin()
function. This reuse technique can save you the time and effort required to reinvent this code yourself.
1. """Pig Latin, by Al Sweigart [email protected]
2. Translates English messages into Igpay Atinlay.
3. This code is available at https://nostarch.com/big-book-small-python-programming
4. Tags: short, word"""
5.
6. try:
7. import pyperclip # pyperclip copies text to the clipboard.
8. except ImportError:
9. pass # If pyperclip is not installed, do nothing. It's no big deal.
10.
11. VOWELS = ('a', 'e', 'i', 'o', 'u', 'y')
12.
13.
14. def main():
15. print('''Igpay Atinlay (Pig Latin)
16. By Al Sweigart [email protected]
17.
18. Enter your message:''')
19. pigLatin = englishToPigLatin(input('> '))
20.
21. # Join all the words back together into a single string:
22. print(pigLatin)
23.
24. try:
25. pyperclip.copy(pigLatin)
26. print('(Copied pig latin to clipboard.)')
27. except NameError:
28. pass # Do nothing if pyperclip wasn't installed.
29.
30.
31. def englishToPigLatin(message):
32. pigLatin = '' # A string of the pig latin translation.
33. for word in message.split():
34. # Separate the non-letters at the start of this word:
35. prefixNonLetters = ''
36. while len(word) > 0 and not word[0].isalpha():
37. prefixNonLetters += word[0]
38. word = word[1:]
39. if len(word) == 0:
40. pigLatin = pigLatin + prefixNonLetters + ' '
41. continue
42.
43. # Separate the non-letters at the end of this word:
44. suffixNonLetters = ''
45. while not word[-1].isalpha():
46. suffixNonLetters = word[-1] + suffixNonLetters
47. word = word[:-1]
48.
49. # Remember if the word was in uppercase or titlecase.
50. wasUpper = word.isupper()
51. wasTitle = word.istitle()
52.
53. word = word.lower() # Make the word lowercase for translation.
54.
55. # Separate the consonants at the start of this word:
56. prefixConsonants = ''
57. while len(word) > 0 and not word[0] in VOWELS:
58. prefixConsonants += word[0]
59. word = word[1:]
60.
61. # Add the pig latin ending to the word:
62. if prefixConsonants != '':
63. word += prefixConsonants + 'ay'
64. else:
65. word += 'yay'
66.
67. # Set the word back to uppercase or titlecase:
68. if wasUpper:
69. word = word.upper()
70. if wasTitle:
71. word = word.title()
72.
73. # Add the non-letters back to the start or end of the word.
74. pigLatin += prefixNonLetters + word + suffixNonLetters + ' '
75. return pigLatin
76.
77.
78. if __name__ == '__main__':
79. main()
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
message.split()
on line 33 to message
?('a', 'e', 'i', 'o', 'u', 'y')
on line 11 to ()
?('a', 'e', 'i', 'o', 'u', 'y')
on line 11 to ('A', 'E', 'I', 'O', 'U', 'Y')
?