We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,520 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Pig Latin translator..

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.

2
Contributors
4
Replies
12 Hours
Discussion Span
4 Years Ago
Last Updated
7
Views
Question
Answered
down with socks
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
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.

down with socks
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You must reinitialize consonant_string before each word.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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

down with socks
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Years Ago by Gribouillis

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0765 seconds using 2.84MB