![]() |
PigLatin Sentence Translator Help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: PigLatin Sentence Translator Help (/thread-11108.html) |
PigLatin Sentence Translator Help - 2skywalkers - Jun-22-2018 What I want me code to do is be able to take a sentence from a user input and output each word in that sentence in Pig Latin form. If I enter "hello there" it says ellohay, it is only printing the first word in pig latin. I am also having trouble with running code in different applications and stuff like that. It won't work in BBEdit or Terminal, but it will in JupterLab. So I would also appreciate it if any could recommend some software for a Mac because I think the software I'm using might be the problem. def pig(): sentence = input("Enter a sentence: ") for word in sentence.split(): first_letter = word[0] if first_letter in "aeiou": word = word + "ay" return word else: word = word[1:] + first_letter + "ay" return word newsent = (" ".join(sentence.split())) print(newsent) pig() RE: PigLatin Sentence Translator Help - gontajones - Jun-22-2018 When you use "return", your function stop there and returns what you specified. RE: PigLatin Sentence Translator Help - 2skywalkers - Jun-22-2018 Thanks for your help, but when I remove the return statements, if I enter "hello there" it prints: hello there hello there RE: PigLatin Sentence Translator Help - gontajones - Jun-22-2018 You have to store the modified "word" in a variable inside the for loop (maybe a list). Then, this part must be outside the for loop: newsent = (" ".join(my_new_words_list)) print(newsent) RE: PigLatin Sentence Translator Help - 2skywalkers - Jun-22-2018 Im not sure if I did it right but its still not working. def pig(): sentence = input("Enter a sentence: ") for word in sentence.split(): first_letter = word[0] newlist = [] if first_letter in "aeiou": word = word + "ay" newlist.append(word) else: word = word[1:] + first_letter + "ay" newlist.append(word) newsent = (" ".join(newlist)) print(newsent) pig() RE: PigLatin Sentence Translator Help - ljmetzger - Jun-22-2018 The following code will help you get started, however you have two remaining tasks: a. Punctuation b. Combining the new words into a sentence def pig(): sentence = input("Enter a sentence: ") for word in sentence.split(): first_letter = word[0] print(word) if first_letter in "aeiou": word = word + "ay" else: word = word[1:] + first_letter + "ay" print(word) print("") newsent = (" ".join(sentence.split())) print(newsent) pig() print("done")Lewis RE: PigLatin Sentence Translator Help - gontajones - Jun-22-2018 You are resetting the newlist every loop iteration. Check this: def pig(): sentence = input("Enter a sentence: ") newlist = [] for word in sentence.split(): first_letter = word[0] if first_letter in "aeiou": word = word + "ay" else: word = word[1:] + first_letter + "ay" newlist.append(word) newsent = (" ".join(newlist)) print(newsent) pig() RE: PigLatin Sentence Translator Help - 2skywalkers - Jun-23-2018 It works, Thanks guys. |