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()
When you use "return", your function stop there and returns what you specified.
Thanks for your help, but when I remove the return statements, if I enter "hello there" it prints:
hello there
hello there
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)
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()
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
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()