Hi I am trying to make a pig-latin translator and a problem arose when I tried to deal with words beginning in vowels. The aim would be that, for words with vowels at the beginning, the suffix would be the first consonant in the word + "ay" (Rather than the first letter in the word + "ay") For example 'ear' would be 'ear-ray' rather than 'ar-eay'
However, I am struggling to find a way to do this.
This i what I have so far: (it is taken from a larger section of code)
elif word[0]=="a" or word[0]=="e" or word[0]=="i" or word[0]=="o" or word[0]=="u":
for letter in word[1:]:
while consonantfound == False:
if letter!="a" and letter!="e" and letter!="i" and letter!="o" and letter!="u":
consonantfound = True
consonant = letter
suffix = consonant + "ay"
newWord = i + "-" + suffix
This made the program just fail to produce an output and continue to run.
I feel like the problem is to do with the sequence of loops but I have experimented with it and no solution presented itself. It could also be something else that I am not aware of.
Do you have a suggestions as to how to make it work?
Any help will be much appreciated.
Try this:
VOWELS = ('a', 'e', 'i', 'o', 'u')
def first_consonant(word):
for letter in word:
if letter.lower() not in VOWELS:
return letter
def pig_latin(word):
return '%s-%say' % (word, first_consonant(word) or word[0])
It tries to find the first consonant in a word and appends ay to the end. If it's not present then it defers to the first letter of the word (I don't know if this is correct, but you can adapt it to your needs).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments