Dec-31-2021, 01:25 PM
Hello,
I'm reading through an introductory Python book and there's a section in which the author presents code for a simple program that converts English into "pig Latin." The program takes user-entered strings and adds "yay" to words that begin with vowels. If a user-entered word begins with a consonant or consonant cluster (like ch or gr), the program moves the consonant or consonant cluster to the end of the word followed by "ay."
Please see below for the code:
The question above may also apply more broadly to any code that reads "len(foo) == 0." If the length of something is 0, doesn't that mean it just doesn't exist? And, if that's so, wouldn't the code be more readable if the in / not operators were used instead?
I'm also unclear on line 15 - why is the program appending prefixNonLetters to pigLatin? And what is the purpose of the continue statement on line 16? If the continue statement wasn't there, how or why would the program run incorrectly?
The introductory Python book that this code is from does explain the general functioning of the program but doesn't seem to address lines 14-16 in particular. So any help would be greatly appreciated. Thanks!
I'm reading through an introductory Python book and there's a section in which the author presents code for a simple program that converts English into "pig Latin." The program takes user-entered strings and adds "yay" to words that begin with vowels. If a user-entered word begins with a consonant or consonant cluster (like ch or gr), the program moves the consonant or consonant cluster to the end of the word followed by "ay."
Please see below for the code:
# English to Pig Latin print('Enter the English message to translate into Pig Latin:') message = input() VOWELS = ('a', 'e', 'i', 'o', 'u', 'y') pigLatin = [] # A list of the words in Pig Latin. for word in message.split(): # Separate the non-letters at the start of this word: prefixNonLetters = '' while len(word) > 0 and not word[0].isalpha(): prefixNonLetters += word[0] word = word[1:] if len(word) == 0: pigLatin.append(prefixNonLetters) continue # Separate the non-letters at the end of this word: suffixNonLetters = '' while not word[-1].isalpha(): suffixNonLetters += word[-1] word = word[:-1] # Remember if the word was in uppercase or title case. wasUpper = word.isupper() wasTitle = word.istitle() word = word.lower() # Make the word lowercase for translation. # Separate the consonants at the start of this word: prefixConsonants = '' while len(word) > 0 and not word[0] in VOWELS: prefixConsonants += word[0] word = word[1:] # Add the Pig Latin ending to the word: if prefixConsonants != '': word += prefixConsonants + 'ay' else: word += 'yay' # Set the word back to uppercase or title case: if wasUpper: word = word.upper() if wasTitle: word = word.title() # Add the non-letters back to the start or end of the word. pigLatin.append(prefixNonLetters + word + suffixNonLetters) # Join all the words back together into a single string: print(' '.join(pigLatin))I think I understand everything that is going on in this program with the exception of the code block on lines 14-16. If I understand it correctly, the if statement in line 14 applies when the length of each user-entered word is 0. But if the user is entering a word, how can the length of it ever be 0 (if they are entering something, doesn't it logically have to have some length to it)? Wouldn't this mean that this if statement would never be satisfied (and therefore this code would never run)?
The question above may also apply more broadly to any code that reads "len(foo) == 0." If the length of something is 0, doesn't that mean it just doesn't exist? And, if that's so, wouldn't the code be more readable if the in / not operators were used instead?
I'm also unclear on line 15 - why is the program appending prefixNonLetters to pigLatin? And what is the purpose of the continue statement on line 16? If the continue statement wasn't there, how or why would the program run incorrectly?
The introductory Python book that this code is from does explain the general functioning of the program but doesn't seem to address lines 14-16 in particular. So any help would be greatly appreciated. Thanks!