Reputation: 77
I am trying to create a program that can convert both from and English sentence into Piglatin and from Piglatin into English.
So far, the English to Piglatin portions are working fine, but I am having trouble converting from Piglatin to English.
def eng2Pig(sentence):
sentsplit = sentence.split()
for part in sentsplit:
print((part[1:] + part[0] + "ay"), end = " ")
def pig2Eng(sentence):
sentv1 = sentence.replace("ay", "")
sentsplit = sentv1.split()
for part in sentsplit:
print(part[-1] + part[:-1], end = " ")
def aySearch(sentence):
numwords = len(sentence.split())
numay = sentence.count("ay ")
if numwords == numay:
pig2Eng(sentence)
else:
eng2Pig(sentence)
x = input("Enter your sentence: ")
x = x + " "
aySearch(x)
I am having troubles converting English words that originally contain ay. For example, today converted to Piglatin would be odaytay. However, I am replacing ay with "" to remove the extra added ay.
Perhaps I need to count the number of ay(s) in a word, then based off of that, decide if I want to remove more than one ay.
Thanks -
Good luck
Upvotes: 0
Views: 138
Reputation: 77
Here is what I ended up doing - this worked well.
def eng2Pig(sentence):
pig = ""
sentsplit = sentence.split()
for part in sentsplit:
pig += (part[1:] + part[0] + "ay" + " ")
return pig
def pig2Eng(sentence):
eng = ""
sentsplit = sentence.split()
for part in sentsplit:
eng += (part[-3] + part[:-3] + " ")
return eng
def aySearch(sentence):
numwords = len(sentence.split())
numay = sentence.count("ay ")
if numwords == numay:
return pig2Eng(sentence)
else:
return eng2Pig(sentence)
def main():
x = input("Enter your sentence: ")
x = x + " "
print(aySearch(x))
main()
Upvotes: 0
Reputation: 168
One problem is doing a replace ("ay", "") will change the word say int s, so it will corrupt your sentence. Here's a better solution.
def pig2Eng(sentence):
eng = ""
sentsplit = sentence.split()
for word in sentsplit:
eng += word[-3:-2] + word[:-3] + " "
return eng
print (pig2Eng("igpay atinlay siay tupidsay"))
Also note that is usually better programming form to return the result rather than printing it in the function.
Upvotes: 1