1,105,534 Community Members

Pig Latin translator..

Member Avatar
down with socks
Newbie Poster
3 posts since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
def translate(response):
    """Translates an English word into Pig Latin."""

    # Initial lists and strings
    vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U", "y", "Y"]
    consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z", "B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Z"]
    consonant_string = ""

    response = response.split()
    for word in response:
            
        for i in vowels:
            if word[0] == i:
                word += "way"

                break

        for x in range(len(word)):
            if word[x] in vowels:
                break
            for i in consonants:
                if word[x] == i:
                    consonant_string += i
        word = word[len(consonant_string):]
        word += consonant_string
        word += "ay"
    return response

An assignment for school was to make a program which would take an English word and translate it to the fake language of Pig Latin. I'm now trying to make it so that it can translate an entire sentence, one word at a time. Because of this, a lot of stuff comes copy and pasted from the original and may not make sense. However, I can not figure out what's wrong here. It just returns the list of words unaltered.

Member Avatar
Gribouillis
Posting Maven
3,456 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

I suggest this

...
    for k,  word in enumerate(response):
        ...
        word += "ay"
        response[k] = word
    return " ".join(response)

The problem is that your code modifies the variable 'word', but not the strings stored in the list. For example

>>> L = ["hello", "world"]
>>> word = L[0]
>>> word += "way"
>>> print(L) # L was not modified
['hello', 'world']

but if we add L[0] = word before print(L) , we get the correct result.

Member Avatar
down with socks
Newbie Poster
3 posts since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
def translate(response):
    """Translates an English word into Pig Latin."""

    # Initial lists and strings
    vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U", "y", "Y"]
    consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z", "B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Z"]
    consonant_string = ""

    response = response.split()
    for k, word in enumerate(response):
            
        for i in vowels:
            if word[0] == i:
                word += "way"
                response[k] = word

                break

        for x in range(len(word)):
            if word[x] in vowels:
                break
            for i in consonants:
                if word[x] == i:
                    consonant_string += i
        word = word[len(consonant_string):]
        word += consonant_string
        word += "ay"
        response[k] = word
    return response

Hmm...okay, that worked, but now I have another problem. May just be my tiredness, but when I enter say "taco bell", the first word comes out correctly, but the second is changed to "lltbay". If I do bell on its own, it comes out fine. I'm guessing it's an error with "break" somewhere but I'm not sure where.

Member Avatar
Gribouillis
Posting Maven
3,456 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

You must reinitialize consonant_string before each word.

Member Avatar
down with socks
Newbie Poster
3 posts since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Amazing how stupid something like that can make you feel. Put that as the first thing in the first for loop and it works swimmingly. Thanks! :D

Question Answered as of 5 Years Ago by Gribouillis
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article