Question: Write a function `is_consonant` that takes a character and returns `True` if it is a consonant. Use your function to create a new function `to_piglatin`
Write a function `is_consonant` that takes a character and returns `True` if it is a consonant.
Use your function to create a new function `to_piglatin` that takes a word, moves all starting consonants (all consonants before the first vowel) to the end of the word, then adds *ay* to the end and returns the result. You may expect that the input to the function will be just one word. (we know this isn't **true** pig latin - please do not change this basic algorithm). For a single word input the first letter is capitalized and the rest are lower case as shown in the example below.
Have the 'to_piglatin' function check whether or not the input is multiple words and return the whole sentence in pig latin. The sentence will be returned in sentence case (only the first word capitalized). For this you may assume that a sentence always ends with a period and the input is always one sentence, never more than one. Make these changes in the 'to_piglatin' function - that is, if you want to do the extra credit the 'to_piglatin' function needs to work for both a single word and a sentence. NOT AS A SEPARATE FUNCTION.*
Examples: ``` (format is: function call -> returns) - do not return this whole string just return the word after the arrow.
to_piglatin('stay') -> Aystay to_piglatin('Jared') -> Aredjay to_piglatin('and') -> Anday to_piglatin('CAR') -> Arcay
Code returning "None"
def is_consonant(char):
if char.lower() in ('a','e','i','o','u'):
return False
else:
return True
def to_piglatin(sentence):
pigLatin = ''
if sentence[-1] == '.': # sentence ends with period
words = sentence.strip().split()
for i in range(0,len(words)-1):
word = words[i]
found = False
for j in range(len(word)):
if not is_consonant(word[j]):
pigLatin = pigLatin + word[j:len(word)] + word[0:j] + 'ay '
found = True
break
if not found:
pigLatin = pigLatin + word[0:len(word)] + 'ay'
word = words[len(words)-1]
found = False
for i in range(len(word)-1):
if not is_consonant(word[i]):
pigLatin = pigLatin + word[i:len(word)-1] + word[0:i] +'ay.'
found = True
break
if not found:
pigLatin = pigLatin + word[0:len(word)-1] + 'ay.'
else:
found = False
for i in range(0,len(sentence)):
if not is_consonant(sentence[i]):
pigLatin = sentence[i:len(sentence)] + sentence[0:i] + 'ay'
found = True
break
if not found:
pigLatin = sentence + 'ay'
pigLatin= pigLatin[0].upper() + pigLatin[1:len(pigLatin)].lower()
return pigLatin
print(to_piglatin('stay'))
print(to_piglatin('Jared'))
print(to_piglatin('and'))
print(to_piglatin('CAR'))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
