PLEASE CODE IN PYTHON Write a program that takes a 3 word phrase and then conver
ID: 3789895 • Letter: P
Question
PLEASE CODE IN PYTHON
Write a program that takes a 3 word phrase and then converts the words to Pig Latin.
To review, Pig Latin takes the rst letter of a word, puts it at the end, and appends “ay”. The only exception is if the rst letter is a vowel, in which case we keep it as it is and append “hay” to the end.
E.g. “boot” “ootbay”, and “image” “imagehay”.
There are many more Pig Latin rules, but for this homework this is sufficient.
Dene a GLOBAL list at the top of your code le called VOWELS. This way, you can check if a letter x is a vowel with the expression x in VOWELS. Remember to get a word except for the rst letter, you can use word[1:].
It’s tricky for us to deal with punctuation and numbers with what we know so far, so instead, ask the user to enter only 3 words and spaces. You need to convert their input from a string to a list. Also convert the 3 word phrase to all lower when doing comparisons against the vowels list.
Using a list of words, you can go through each word and convert it to Pig Latin by calling the function ConvertWordToPigLatin. Make sure you have a check in your code that it only converts lists that are of length 3 to Pig Latin.
Break all the tasks into functions. Should have functions that: reads user input with name AskUserForSentence, lowercases the sentence called LowercaseSentence, splits the phrase called SplitSentenceIntoList, converts the word to pig latin called ConvertWordToPigLatin and prints out the 3 word phrase in pig latin called PrintThreeWordPhrase. Remember, using functions makes your code much more re-usable, readable and clean.
Once you have your program working, make it interactive such that it keeps translating 3 word phrases into pig latin until the user enters in the phrase QUIT.
Explanation / Answer
VOWELS = ('a', 'e', 'i', 'o', 'u')
def ConvertWordToPigLatin(word):
first_letter = word[0]
if first_letter in VOWELS: # if word starts with a vowel...
return word + "hay" # keep it as it is and add hay to the end
else: #Otherwise
return word[1:] + word[0] + "ay" # like the lab mentioned, word[1:]
# returns the word except word[0]
# From this function, it's easy to take a sentence and convert it to Pig-Latin.
def SplitSentenceIntoList(sentence):
list_of_words = sentence.split(' ')
new_sentence = "" # we will keep concatenating words to this...
for word in list_of_words:
new_sentence = new_sentence + ConvertWordToPigLatin(word) # ...like this
new_sentence = new_sentence + " " # but do not forget the space!
return new_sentence
# Now, let's write the main program code, to ask the user and convert.
main():
print "Type in a sentence, and it'll get converted to Pig-Latin!"
print "Please don't use punctuation or numbers."
print "Also, we can't handle uppercase/lowercase yet, so lowers only please!"
print
text = AskUserForSentence()
print
print SplitSentenceIntoList(text)
def AskUserForSentence():
text = raw_input()
return text
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.