python question! please show code and steps! Lab Question 4 (Pig Latin) [5 point
ID: 3668406 • Letter: P
Question
python question!
please show code and steps!
Lab Question 4 (Pig Latin) [5 points] with bonus 12 points]: Write a function igpayatinlay (e) that translates English to Pig Latin: for words that begin with a consonant, all initial consonants are moved to the end of the word and Tay" s appended. For ords that begin with a vowel, simply "way s appended: igpayatinlay 'What is the airspeed velocity of an unladen swallow? 'atwhay isway ethay airspeedway elocityvay of way anway unladenway allows way?' Words consist only of lowercase characters; everything else, in particular punctuation and space, separates words and is preserved. See the Wikipedia page at http://en.wikipedia.org/wikiP Latin for more examples You can also practice Pig Latin using Google httpse atin As a bonus question write a function iglatinpluspay (e) that additionally takes capitalization into account: if a word is capitalized, the first letter remains capitalized and all other letters are lowercase (this is what Google does): igpayatinlay 'What is the airspeed velocity of an unladen swallow? Atwhay isway ethay airspeedway elocityvay ofway anway unladenway allows way?' The functions should return strings, not print them. Place both functions in the file 24 yourmacid.pyExplanation / Answer
vowels = {'a', 'e', 'i', 'o', 'u'}
consonants = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'}
def igpayatinlay(word):
suffix = 'ay'
if word[0] in vowels:
suffix = 'way'
end = 1
if word[0] not in vowels:
for i in range(1,len(word)):
if word[i] in vowels:
end = i
break
return word[end:len(word)]+word[0:end]+suffix
# This program translates sentences into Pig Latin
def pig_latin(sentence):
sentence = sentence.split(" ")
output = ''
vowels = ['a', 'e', 'i', 'o', 'u']
for word in sentence:
for letter in range(len(word)):
if word[letter] == 'q':
letter += 2
break
if(word[letter] in vowels):
break
if(word.lower() == word):
output += word[letter:] + word[:letter].lower() + 'ay'+ ' '
else:
if word[letter:] != '':
output += word[letter:].capitalize() + word[:letter].lower() + 'ay'+ ' '
else:
output += word[:letter].capitalize() + 'ay'+ ' '
print(output)
def main():
another = 'y'
while(another == 'y'):
entered = str(input("Input a string which you would like to turn into a Pig Latin sentence: "))
pig_latin(entered)
another = str(input("Do you want to translate another sentence? (y/n): "))
print('')
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.