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()

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

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.

Got it to work thanks for the help.

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.