Needs to be written using python. thanks for your help in advance. Pig Latin Tra
ID: 3788187 • Letter: N
Question
Needs to be written using python. thanks for your help in advance.
Pig Latin Translator Prompt the user with the following question: "Hi. Please select an option: Convert word to Pig Latin Convert word from Pig Latin If the user selects 1, then prompt the user for the word to be converted. Convert the word Print to the screen: "Original word: " original word "Word in Pig Latin: " new word If the user selects 2, then prompt the user for the word to be converted. Make sure that the last 2 characters of the word are "ay". Otherwise, ask for a new word until they do. Convert the word Print to the screen "Original word in Pig Latin: " original word "Word in English: " new word If the user enters something other than 1 or 2, exit the program.Explanation / Answer
def main():
to_piglatin = input("Hi. Please select the option: 1: Convert word to PigLatin[P], 2: Convert word to English[E]: ")
while to_piglatin not in("p", "e"):
to_piglatin = input("[P/E]")
to_piglatin = ("e", "p").index(to_piglatin.lower())
if to_piglatin:
lang = "pig latin"
else:
lang = "english"
question = "Enter a sentence to translate it into {}: ".format(lang)
sentence = input(question)
if not sentence.isalpha():
print("Invalid input, please do not use numbers or punctuation marks")
sleep(1)
main()
split_sentence = sentence.split()
if to_piglatin:
translated_sentence = [translate_to_piglatin(word) for word in split_sentence]
else:
translated_sentence = [translate_to_english(word) for word in split_sentence]
translated_sentence = " ".join(translated_sentence)
print(translated_sentence)
input()
return True
def translate_to_piglatin(word):
vowels = 'aeiou'
list_word = list(word)
list_word = [x.lower() for x in word]
first_letter = list_word[0]
last_letter = list_word[-1]
if last_letter.isalpha() == False:
list_word.remove(last_letter)
if first_letter in vowels:
list_word.append('y')
list_word.append('a')
list_word.append('y')
else:
list_word.remove(list_word[0])
list_word.append(first_letter)
list_word.append('a')
list_word.append('y')
translated_word = "".join(list_word)
return translated_word
def translate_to_english(word):
list_word = list(word)
last_three_letters = list_word[-3:]
last_three_letters = "".join(last_three_letters)
if last_three_letters == 'yay':
vowel = True
else:
vowel = False
if vowel:
for i in range(0,2):
del list_word[-1]
else:
del list_word[-1]
del list_word[-1]
last_letter = list_word[-1]
list_word.remove(last_letter)
list_word.insert(0, last_letter)
translated_word = "".join(list_word)
return translated_word
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.