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.

Recommended Answers

All 4 Replies

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.

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.

You must reinitialize consonant_string before each word.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.