0
\$\begingroup\$

I have written a program on Pig Latin converter which I've attached herewith Pastebin. I am still a beginner, and I know that the code works perfectly, but can we make some improvements in this code cause I don't like using break statements too much. Also, is there any way to make this code smaller. Looking for your kind suggestions. Thanks.

'''Pig Latin is a language constructed by transforming English words. While the ori-
 gins of the language are unknown, it is mentioned in at least two documents from
 the nineteenth century, suggesting that it has existed for more than 100 years. The
 following rules are used to translate English into Pig Latin:
• If the word begins with a consonant (including y), then all letters at the beginning of
the word, up to the first vowel (excluding y), are removed and then added to the end
of the word, followed by ay. For example, computer becomes omputercay
and think becomes inkthay.
• If the word begins with a vowel (not including y), then way is added to the end
of the word. For example, algorithm becomes algorithmway and office
becomes officeway.
Write a program that reads a line of text from the user. Then your program should
translate the line into Pig Latin and display the result. You may assume that the string
entered by the user only contains lowercase letters and spaces.

'''


def pig_latin(word):
    word = word.strip().lower()
    const_tail = 'ay'
    vow_tail = 'way'
    pig_latin =''
    vowel = ['a','e','i','o','u']
    for i in range(len(word)):
        if word[0] in vowel:
            pig_latin+=word+vow_tail
            break
        else:
            if word[i] in vowel:
                pig_latin+=word[i:]+word[0:i]+const_tail
                break
return pig_latin

def main():
    word = str(input('Enter the word: '))
    print('The pig latin translation of the string is',pig_latin(word))


if __name__ == "__main__":
    main()
\$\endgroup\$
1
  • \$\begingroup\$ Please check your indentation. This is Python. If the indentation is off, the code doesn't work. I strongly suspect everything from the line with the if to the return need another level. \$\endgroup\$
    – Mast
    Commented Apr 21, 2020 at 9:12

1 Answer 1

2
\$\begingroup\$

Unnecessary Check

We don't have to check the first letter every iteration, let's treat this any other letter in the word until it matters, then check our index. Once we find a vowel(if there is one) we then check if it was our first letter to determine if we use the vowel tail or not. Then break after the index condition checks.

def pig_latin(word):
    word = word.strip().lower()
    const_tail = 'ay'
    vow_tail = 'way'
    pig_latin =''
    vowel = ['a','e','i','o','u']
    for i in range(len(word)):
        if word[i] in vowel:
            if i==0:
                pig_latin+=word+vow_tail
            else:
                pig_latin+=word[i:]+word[0:i]+const_tail
            break
    return pig_latin

Additional Code Simplification

By only adding the W to the end of a string starting with a vowel we can also eliminate the else case and apply it's code to every string that makes it there.

Also an option, but not shown below is removing the break and returning the restructured string from within the for.

def pig_latin(word):
    word = word.strip().lower()
    pig_latin =''
    vowel = ['a','e','i','o','u']
    for i in range(len(word)):
        if word[i] in vowel:
            if i==0:
                word+="w"
            pig_latin+=word[i:]+word[0:i]+"ay"
            break
    return pig_latin
\$\endgroup\$
1
  • \$\begingroup\$ Thanks, it looks very good now. I hope I am heading towards the right path now. \$\endgroup\$ Commented Apr 21, 2020 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.