Having issues with my English to PigLatin Converter. I can get the code to execute correctly on one word but once i do 2 or more words it adds the first letter [0] + 'ay' to the last word in the line.... any suggestions?

def main():
    # Create a list of words
    words = []
    # user input a sentence.
    sentence = input("Please enter a sentence in english to convert to 'Pig Latin': ")
    
    # for loop that splits up sentence and appends it to words list
    for wrd in sentence.split():
        wrd = words.append

    # Display the Sentence in english to the user
    print("Here is the sentence you entered: ", sentence)
    # Dsiplay the new sentence in PigLatin
    print(PigLatinConverter(sentence))

    
def PigLatinConverter(words):
    # Create a for loop to iterate of words
    for i in range(len(words)):
        # Add the first letter and ay to the end
        PigLatin = words[1:] + words[0] + "ay"
    # return PigLatin to main function
    return PigLatin

# Calls the main function to execute
main()

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

You are passing sentence not words. You do not need to store append method zillion times to wrd.

Edited 5 Years Ago by pyTony: n/a

Please use the CODE button to show indents, line numbers, syntax coloring...

Hints:

  • You can get the list of words by simply saying words = sentence.split()
  • You need to think about punctuation, in case user inputs some. For instance, if you got the words from the first sentence of this hint, "punctuation," and "some." have trailing punctuation.
  • I would write the function english_to_piglatin so the parameter is a single word rather than a list of words.
  • The usual idiom for modules that need to (also) run as a script is if __name__ == "__main__": main() You might as well get into the habit of invoking main() that way.

Edited 3 Years Ago by happygeek: fixed formatting

This question has already been answered. Start a new discussion instead.