Member Avatar for Member #1132231

Hello, i recently learned Pig Latin and how it works (some stupid hard shakespeare language in Python lol) and in CodeAcademy it says "print Pig Latin" in pig latin which i do not know how and i need some help, here is the code i entered for the program on the site as i copied it and learned it from Youtube vids, then modified it on pycharm and enjoyed it, Please help XD
(my first post)

def main():
        lst = ['sh', 'gl', 'ch', 'ph', 'tr', 'br', 'fr', 'bl', 'gr', 'st', 'sl', 'cl', 'pl', 'fl']
        sentence = input('Type what you would like translated into pig-latin and press ENTER: ')
        sentence = sentence.split()
        for k in range(len(sentence)):
                i = sentence[k]
                if i[0] in ['a', 'e', 'i', 'o', 'u']:
                        sentence[k] = i+'ay'
                elif t(i) in lst:
                        sentence[k] = i[2:]+i[:2]+'ay'
                elif i.isalpha() == False:
                        sentence[k] = i
                else:
                        sentence[k] = i[1:]+i[0]+'ay'
        return ' '.join(sentence)

def t(str):
        return str[0]+str[1]

if __name__ == "__main__":
        x = main()
        print(x)

Dani AI

Generated

A short clarification and a safer approach that builds on the thread: @pastapengus later confirmed the intent is an interactive Pig‑Latin translator, and was right to suggest a loop. The posted code has a few fragile points worth fixing: the helper that returns the first two chars will IndexError on one-letter tokens or punctuation, indexing is done before checking for non-letters, and naming a parameter str shadows the built-in. The translator should also preserve leading/trailing punctuation and capitalization and handle contractions and hyphenated words sensibly.

Recommended steps (algorithm): tokenize so leading/trailing punctuation is kept separate; for the alphabetic core, detect the initial consonant cluster (everything up to the first vowel), treat "qu" as part of that cluster, and then:

  • if the word starts with a vowel, append "ay";
  • otherwise move the cluster to the end and append "ay";
  • restore capitalization and reattach punctuation.
    Reassembling tokens while preserving whitespace gives a natural, interactive experience and avoids IndexError.

Example implementation (robust, avoids the earlier indexing bug):

import re

VOWELS = "aeiou"

def pig_word(token):
    m = re.match(r"^([^A-Za-z]*)([A-Za-z][A-Za-z'-]*)([^A-Za-z]*)$", token)
    if not m:
        return token
    lead, core, trail = m.groups()
    lower = core.lower()
    if lower[0] in VOWELS:
        transformed = lower + "ay"
    else:
        m2 = re.match(r"^([^aeiou]+)(.*)$", lower)
        cons, rest = m2.groups() if m2 else ("", lower)
        if cons.endswith("q") and rest.startswith("u"):
            cons += "u"; rest = rest[1:]
        transformed = (rest + cons + "ay") if cons else (lower[1:] + lower[0] + "ay")
    if core[0].isupper():
        transformed = transformed.capitalize()
    return lead + transformed + trail

def pig_sentence(s):
    parts = re.split(r"(\s+)", s)
    return "".join(pig_word(p) if not p.isspace() else p for p in parts)

if __name__ == "__main__":
    while True:
        text = input("Type text (blank to quit): ")
        if text == "":
            break
        print(pig_sentence(text))

Troubleshooting notes: test short words, punctuation-only tokens, and contractions; check any helper that indexes without a length guard (the cause of earlier crashes); and prefer preserving whitespace (the code above does that). This complements 's loop suggestion while avoiding the IndexError and preserving punctuation/capitalization.

Recommended Answers

All 4 Replies

And your question is?

@pastapengus
Your Question is not clear.... do you need help or you post the code for other to make use of it.... Pls tell the house what you want or need

Member Avatar for Member #1132231

I mean , how do i make an interactive program with Pig Latin

If you want to translate more than one sentence, you can use a loop

if __name__ == "__main__":
    while True:
        x = main()
        print(x)
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.