Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code:
#Problem: Move the first letter of each word to the end of it, then add #"ay" to the end of the word. Leave punctuation marks untouched.

#Example: "The train is fast !" --> "heTay raintay siay astfay !"

def pig_it(text):
    import string
    words = text.split()
    for word in words:
        first_letter = word[0]
        word += first_letter
        word = word[1:]
        if word not in string.punctuation:
            word += ("ay")
    new_words = ((" ").join(words))
    return(new_words)


pyg_latin = pig_it("The train is fast !")
print(pyg_latin)


The output returns "The train is fast !" when it should return the pyg latin value.

What I have tried:

I tried debugging and I got some results. during the for loop, it looks like each word (element) gets changed to its respective pyg-latin value correctly, but after the for loop, the words list gets changed back to its original value.
Posted
Updated 8-Jul-23 13:14pm

1 solution

You are modifying word but not saving the changes. Try

Python
import string

def pig_it(text):
    pig = []
    for word in text.split():
        if word not in string.punctuation:
            pig.append(word[1:] + word[0] + 'ay')

    return ' '.join(pig)


pyg_latin = pig_it("The train is fast !")
print(pyg_latin)
 
Share this answer
 
v2
Comments
Naveen Krishna1 8-Jul-23 19:30pm    
Thanks dude!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900