I am trying to write a program in Python that converts user input from English to Pig Latin ("Y" is not a vowel in this case, breaks off at first vowel and moves preceding consonants to the end). The end product of my program should be able to compute whole sentences, but right now im just trying to get it to do one word. This is what I currently have but whenever a string is inputted that does not begin with a vowel it goes through and performs the function for every instance of a vowel. For example:
Enter a word:hello
ellohay
ohellay

Please help?!

defining variables to be used later on

pyg = 'ay'
vowels = "aeiou"

get user input

original = raw_input('Enter a word:')

def main():

# make sure only alphabetic word is entered
if len(original) > 0 and original.isalpha():
    word = original.lower() # change to lower case input

    for word in original.split():
        if word[0] in vowels: # find if first letter is vowel
            new_word = word + pyg
            print new_word
        else:
            for i in range(1, len(word)): # search word for first instance of vowel
                if word[i] in vowels:
                    # splice word so that the consonants preceding the first vowel move to the end
                    new_word = word[i:] + word[:i] + pyg
                    print new_word
else: # if input is not alphabetic
    print 'That is not a word!'

main()

Dani AI

Generated

The bug is that the code prints a result inside the inner loop that looks for vowels, so every vowel triggers a print. Stop searching once the first vowel is found (break or return), process one word at a time, collect results, then join them for a sentence. ’s example shows the symptom; is correct that splitting and joining is the right approach for full sentences.

VOWELS = "aeiou"   # 'y' intentionally excluded
SUFFIX = "ay"

def pig_latin_word(word):
    if not word:
        return word
    lower = word.lower()
    # find index of first vowel (or None)
    idx = next((i for i, ch in enumerate(lower) if ch in VOWELS), None)
    if idx is None:               # no vowel found
        out = lower + SUFFIX
    elif idx == 0:                # starts with vowel
        out = lower + SUFFIX
    else:                         # consonant cluster moved to end
        out = lower[idx:] + lower[:idx] + SUFFIX
    # preserve simple capitalization
    return out.capitalize() if word[0].isupper() else out

def pig_latin_sentence(s):
    return " ".join(pig_latin_word(w) for w in s.split())

Notes and quick tips:

  • Avoid printing inside the vowel-search loop; build the transformed word once and then stop (the code above uses next(...) so only the first vowel matters).
  • Don’t reuse the same variable name for different purposes (e.g., assigning word = original and then for word in ...); use separate names or local function scope.
  • For real sentences consider preserving punctuation and contractions (split() is fine for simple cases, but use a regex tokenizer to keep commas/periods attached or to handle "don't", hyphenated words, etc.).
  • Decide how to handle words with no vowels (example above appends the suffix).

Recommended Answers

All 2 Replies

ok i figured out how to get it to stop after one by adding a while loop:

else:
                counter = 0
                while counter == 0:
                    for i in range(1, len(word)): # search word for first instance of vowel
                        if word[i] in vowels:
                            # splice word so that the consonants preceding the first vowel move to the end
                            new_word = word[i:] + word[:i] + pyg
                            print new_word
                            counter = counter + 1
                        else:
                            return

but now i have no clue of how to repeat this for every word in a sentence. i know how to use the split function, but i dont know how to call upon every word that is outputted after a split. i.e.

string = "hello world"
string.split()
>>> [hello, world]

right?

you can repeat your code for every word in string.split then join them at the end.

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.