Reputation:
I am a beginner in Python, and I want my code to move the first one or two consonants to the end of the word and add -ay. I have gotten my code to do this but it doesn't work for sentences. I need help with the list. Also, the returned, translated sentence needs to start with an uppercase letter and end with a period, just like the input sentence but I have no clue where to start with this! Here is my code so far:
pyg = 'ay'
original = raw_input('Enter a sentence:')
word = original.lower()
word_list = word.split
count = 0
for word in word_list:
if word[1] == "a" or word[1] == "e" or word[1] == "i" or word[1] == "u" or
word[1] == "o" or word[1] == "y":
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:len(new_word)]
print new_word
else:
if word[1] != "a" or word[1] != "e" or word[1] != "i" or word[1] != "u" or
word[1] != "o" or word[1] != "y":
first = word[0]
second = word[1]
second_word = word + first + second + pyg
second_word = second_word[2:len(second_word)]
print second_word
Upvotes: 0
Views: 2190
Reputation: 11
I have tried creating the code using .format() , being beginner myself , I hope you find this code easy & helpful.
def pig_latin(text):
say = ""
words = text.split()
for word in words:
say += "{}{}{} ".format(word[1:], word[0], "ay")
return say
print(pig_latin("hello how are you"))
ellohay owhay reaay ouyay
Upvotes: 1
Reputation: 16952
This is a slightly generalized version of what you say you want. The difference is that the code below doesn't look at the first one or two letters: it shifts all initial consonants to the end, so that string
becomes ingstray
. Your problem statement says that should be ringstay
, but that doesn't accord with the Pig Latin I learnt as a child. I've done this partly because I think your specification is incomplete, and partly because it makes the code simpler, and that makes it easier to understand. You can always amend the code if ringstay
is really what you want. (Hint: put a counter in the while
loop.) There is also a bug in this code, though you would be unlikely to find it: if an input word contains no vowels at all, for example cwm (a kind of Welsh fiddle) then the while
loop will never terminate. (Hint: same solution.)
I haven't added the refinement, also dimly remembered from my childhood, that an original word that begins with a vowel gets the suffix way
not ay
, so I am good
comes out as Iway amway oodgay
not Iay amay oodgay
.
The Pythonic way to do a membership test is to use in
, not a long string of if x == 'a' or x == 'b' or x == 'c'
tests.
Providing an initial capital and a final fullstop requires you to split the problem into two: (1) transform the words, and (2) punctuate the sentence. You can't do that if you print
the transformed words out as you go along. So the code below assembles the words into a result
list and then strings the list of words into a final sentence for punctuation.
pyg = 'ay'
original = raw_input('Enter a sentence:')
word_list = original.lower().split()
result = []
for word in word_list:
while word[0] not in "aeiouy":
word = word[1:] + word[0]
word += pyg
result.append(word)
final_sentence = " ".join(result)
print final_sentence[0].upper() + final_sentence[1:] + "."
Personally I consider it not good style to store a sentence in a variable called word
, especially if the code actually stores a word in that same variable at a later point. So I've amended that too.
Edit since it appears my hint wasn't explicit enough:
To limit the number of consonants that get shifted to the end, you need to count them:
for word in word_list:
consonants = 0
while word[0] not in "aeiouy" and consonants < 2:
word = word[1:] + word[0]
consonants += 1
word += pyg
result.append(word)
To make sure the program isn't put into an infinite loop by a word with no consonants:
while word[0] not in "aeiouy" and consonants < len(word):
Upvotes: 0