Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Python) If Else issue and list to string conversion issue

I'm fairly new to programming and I've been assigned a homework assignment that converts English text into Pig Latin.

The code I have so far is:

VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
def vowel_start(word):
     pig_latin = word + "ay"
     return pig_latin
def vowel_index(word):
     for i, letters in enumerate(word):
          if letters in VOWELS:
               vowel_index = i
               pig_latin = word[vowel_index:] + word[:vowel_index] + "ay"
               return pig_latin
          else:
               pig_latin = word   #The issue here is that even if the word
               return pig_latin   #has vowels, the program will still only
                                  #return the untranslated word as shown
                                  #in else.
english_text = raw_input("What do you want to translate?")
translate = english_text.split()

pig_latin_words = []
translated_text = "".join(str(pig_latin_words)) #The issue here is that the list
                                                #will not join with the string.

for i in translate:
     first = i[0]
     vow = False
     if first in VOWELS:
          vow = True
     if vow == True:
          pig_latin_words.append(vowel_start(i))
     else:
          pig_latin_words.append(vowel_index(i))

print "The text you translated is " + english_text
print "The translated text is " + translated_text #The issue here is that the program
                                                  #displays "The translated text is "
                                                  #and that's it

If I comment out the else aspect of the def vowel_index function, the if aspect then works. If I leave it in the program, the if aspect no longer functions. I've been trying to fix this the past few days and I have no idea how to fix it. Any help would be appreciated, thanks!

For more detail on the assignment:

  • If the word begins with the vowel, leave the word as is and add "ay" to the end.
  • If the word contains a vowel, but not in the beginning, take the letters before the vowel, move it to the end of the word and add "ay" to the end.
  • If the word does not contain any vowels, then leave the word as is.
like image 868
David Chang Avatar asked Feb 26 '13 17:02

David Chang


3 Answers

The way your function is currently written, you will always return on the first iteration of the for loop based on the first character being a vowel or not. You need to loop through every character and only return the word unchanged if none of the characters were vowels.

Start by removing the else; you don't want to return when you see a single character that is not a vowel, since later characters may be a vowel. Now since you have a return statement in your if, you know that if you reach the end of the loop without returning that none of the characters in word was a vowel, so you can just return the word unchanged outside of the for loop. For example:

def vowel_index(word):
    for i, letters in enumerate(word):
        if letters in VOWELS:
            vowel_index = i
            return word[vowel_index:] + word[:vowel_index] + "ay"
    return word

For your second issue, there are a couple of things going on here.

First, you need to move the creation of translated_text so that it is after pig_latin_words already has the new words. This would be just before your print statements at the end.

Also, to convert the list of words to a single string separated by spaces you should use the following:

translated_text = " ".join(pig_latin_words)

Here is a brief example showing the difference:

>>> pig_latin_words = ['atcay', 'ogday']
>>> print "".join(str(pig_latin_words))  # your version
['atcay', 'ogday']
>>> print " ".join(pig_latin_words)      # my version
atcay ogday
like image 194
Andrew Clark Avatar answered Oct 22 '22 21:10

Andrew Clark


The first issue:

def vowel_index(word):
     for i, letters in enumerate(word):
          if letters in VOWELS:
               vowel_index = i
               pig_latin = word[vowel_index:] + word[:vowel_index] + "ay"
               return pig_latin
          else:
               pig_latin = word  
               return pig_latin   

If you have the word 'BA', your loop will set letters to 'B' the first time, then execute the else, and return the word. You need to look for the first vowel before returning anything.

Your second issue:

translated_text = "".join(str(pig_latin_words))

This should probably be:

translated_text = "".join(pig_latin_words)

This is because join expects a list and you were passing it a string. I also notice that translated_text is set to an empty string. You need to put something in that list.

like image 35
munk Avatar answered Oct 22 '22 20:10

munk


The other answers address your first concern. I think you can solve your second and third problems by moving this line translated_text = "".join(str(pig_latin_words)) to after the for loop:

pig_latin_words = []

for i in translate:
     first = i[0]
     vow = False
     if first in VOWELS:
          vow = True
     if vow == True:
          pig_latin_words.append(vowel_start(i))
     else:
          pig_latin_words.append(vowel_index(i))

translated_text = " ".join(pig_latin_words) #The issue here is that the list
                                                #will not join with the string.

print "The text you translated is " + english_text
print "The translated text is " + translated_text #The issue here is that the program
                                                  #displays "The translated text is "
                                                  #and that's it

And as elsewhere mentioned you can also remove the str() from around pig_latin_words. I also think you'd want to join them with a space instead of an emtpy string: " ".join()

like image 30
askewchan Avatar answered Oct 22 '22 22:10

askewchan