Please type a word: hello ellohay Please type a word: goodbye oodbyegay Please type a word: control-d or control-z enter to signal end of input
Please type a word: Traceback (most recent call last): File "/Users/myname/python/subscript.py", line 20, in <module> firstCharacter = word[0] IndexError: string index out of range
while True: try: word = input("Please type a word: ") except EOFError: sys.exit(0) if len(word) == 0: #or if not word: firstCharacter = "" rest = "" else: firstCharacter = word[0] rest = word[1:] pigLatin = rest + firstCharacter + "ay" print(pigLatin)
Please type a word: return or enter ay Please type a word: b bay Please type a word: control-d or control-z
\
,
see
Explicit
line joining.
while True: try: word = input("Please type a word: ") except EOFError: sys.exit(0) if not word: leadingConsonant = "" rest = "" else: #If the word starts with a vowel, temporarily prefix "w" to the word. if word[0] == "a" or word[0] == "e" or word[0] == "i" or word[0] == "o" \ or word[0] == "u": word = "w" + word leadingConsonant = word[0] rest = word[1:] pigLatin = rest + leadingConsonant + "ay" print(pigLatin)
Please type a word: apple appleway Please type a word: banana ananabay Please type a word: etc.
#If the word starts with a vowel, temporarily prefix "w" to the word. if word[0] == "a" or word[0] == "e" or word[0] == "i" or word[0] == "o" \ or word[0] == "u": word = "w" + wordto
#If the word starts with a vowel, temporarily prefix "w" to the word. if word[0] in "aeiou": word = "w" + word
Please type a word: apple appleway Please type a word: banana ananabay Please type a word: etc.
The yellow section uses the
LBYL
style
(rather than
EAFP)
to check if the end of the string has been reached before we attempt to use the
[ ]
.
Why does this section use
while
instead of
for
?
Well, suppose the word was
CBS
.
In this case, the yellow code should set
i
to 3.
But the loop
for i in range(len(word)):
could set
i
no higher than 2.
while True: try: word = input("Please type a word: ") except EOFError: sys.exit(0) if not word: leadingConsonants = "" rest = "" else: #If the word starts with a vowel, temporarily prefix "w" to the word. if word[0] in "aeiou": word = "w" + word i = 0 while i < len(word) and word[i] in "bcdfghjklmnpqrstvwxz": i += 1 #At this point, i is the number of leading consonants in the word. leadingConsonants = word[:i] #the first i characters rest = word[i:] #the rest of the word pigLatin = rest + leadingConsonants + "ay" print(pigLatin)
Please type a word: rump umpray Please type a word: trump umptray Please type a word: string ingstray Please type a word: schmuck uckschmay
Another way to find the number of leading consonants in the word:
import re #regular expressions
m = re.match(r"^([bcdfghjklmnpqrstvwxz]*)", word) #m is a Match. i = len(m.group(1))
word[0].isupper()
True
or
False
.
while True: try: word = input("Please type a word: ") except EOFError: sys.exit(0) if not word: capitalize = False leadingConsonants = "" rest = "" else: capitalize = word[0].isupper() #Make a mental note. word = word.lower() #If the word starts with a vowel, temporarily prefix "w" to the word. if word[0] in "aeiou": word = "w" + word i = 0 while i < len(word) and word[i] in "bcdfghjklmnpqrstvwxz": i += 1 #At this point, i is the number of leading consonants in the word. leadingConsonants = word[:i] #the first i characters rest = word[i:] #the rest of the word pigLatin = rest + leadingConsonants + "ay" if capitalize: pigLatin = pigLatin.capitalize() print(pigLatin)
Please type a word: trump umptray Please type a word: Trump Umptray
The yellow sections use the LBYL style (rather than EAFP) to check if the end of the string has been reached.
while True: try: sentence = input("Please type a sentence: ") except EOFError: sys.exit(0) while sentence: #or while len(sentence) > 0: i = 0 while i < len(sentence) and not sentence[i].isalpha(): i += 1 #At this point, i is the number of leading non-letters #(spaces, punctuation marks, etc). leadingNonletters = sentence[:i] sentence = sentence[i:] #Remove the leading non-letters. print(leadingNonletters, end = "") if not sentence: #nothing left to translate break i = 0 while i < len(sentence) and sentence[i].isalpha(): i += 1 #At this point, i is the number of leading letters in the sentence. word = sentence[:i] #the leftmost surviving word in the sentence sentence = sentence[i:] #Remove the leading word. if not word: capitalize = False leadingConsonants = "" rest = "" else: capitalize = word[0].isupper() word = word.lower() #If the word starts with a vowel, temporarily prefix "w" to the word. if word[0] in "aeiou": word = "w" + word i = 0 while i < len(word) and word[i] in "bcdfghjklmnpqrstvwxz": i += 1 #At this point, i is the number of leading consonants in the word. leadingConsonants = word[:i] #the first i characters rest = word[i:] #the rest of the word pigLatinWord = rest + leadingConsonants + "ay" if capitalize: pigLatinWord = pigLatinWord.capitalize() print(pigLatinWord, end = "") #Output a word translated into Pig Latin. print() #Output a newline at the end of the translated sentence.
Please type a sentence: Read my lips: "No new taxes!" Eadray ymay ipslay: "Onay ewnay axestay!"
while
loop would be easier to read if we took the code that translates one word
into Pig Latin
and packaged it separately as a
user-defined
function.
def pigLatin(word): """ Return the Pig Latin translation of the word. """ assert isinstance(word, str) if not word: capitalize = False leadingConsonants = "" rest = "" else: capitalize = word[0].isupper() word = word.lower() #If the word starts with a vowel, temporarily prefix "w" to the word. if word[0] in "aeiou": word = "w" + word i = 0 while i < len(word) and word[i] in "bcdfghjklmnpqrstvwxz": i += 1 #At this point, i is the number of leading consonants in the word. leadingConsonants = word[:i] #the first i characters rest = word[i:] #the rest of the word pigLatinWord = rest + leadingConsonants + "ay" if capitalize: pigLatinWord = pigLatinWord.capitalize() return pigLatinWord while True: try: sentence = input("Please type a sentence: ") except EOFError: sys.exit(0) while sentence: i = 0 while i < len(sentence) and not sentence[i].isalpha(): i += 1 #At this point, i is the number of leading non-letters #(spaces, punctuation marks, etc). leadingNonletters = sentence[:i] sentence = sentence[i:] print(leadingNonletters, end = "") if not sentence: #nothing left to translate break i = 0 while i < len(sentence) and sentence[i].isalpha(): i += 1 #At this point, i is the number of leading letters in the sentence. word = sentence[:i] #the leftmost surviving word in the sentence sentence = sentence[i:] print(pigLatin(word), end = "") print() #Output a newline at the end of the translated sentence.