Python Forum
"If Len(Word) == 0" Code Block in Pig Latin Program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"If Len(Word) == 0" Code Block in Pig Latin Program
#1
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:

# 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!
Reply
#2
Think about what happens if the user just hits "Enter" without typing in a word: input returns an empty string (i.e. a string of length 0). Similarly, one can have collections (e.g. lists, sets, etc.) that are empty.
Reply
#3
Got it, thank you ndc85430.
Reply
#4
It could be that or it could be to just skip any non-alphabetic "words" such as numbers. In "Welcome 2022!" you don't want 2022! to be altered. When this code processes "2022!":
     while len(word) > 0 and not word[0].isalpha():
          prefixNonLetters += word[0]
          word = word[1:]
It will move all the characters from word to prefixNonLetters and you'll end up with prefixNonLetters == "2022!" and word == "". len(word) == 0, so append "2022!" to pigLatin and move on to the next word.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  non-latin characters in console from clipboard Johanson 3 713 Oct-26-2023, 10:10 PM
Last Post: deanhystad
  python multiple try except block in my code -- can we shorten code mg24 10 6,177 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,518 Aug-12-2021, 04:25 PM
Last Post: palladium
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 2,345 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  Some Code For a program PythonCodeMaker_119 2 2,342 Apr-14-2021, 08:47 PM
Last Post: BashBedlam
  Pressing non-latin characters? Murlog 0 1,539 Jul-25-2020, 03:10 PM
Last Post: Murlog
  Modify code from running program ? samuelbachorik 2 2,455 Jun-26-2020, 08:17 PM
Last Post: samuelbachorik
  Python Speech recognition, word by word AceScottie 6 16,028 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Block of code, scope of variables and surprising exception arbiel 8 3,443 Apr-06-2020, 07:57 PM
Last Post: arbiel
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,881 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020