Question: For this assignment, we have to take the code below and reverse it to make a PigLatin to English translator for Python. '''iglatinpay anslatortray if

For this assignment, we have to take the code below and reverse it to make a PigLatin to English translator for Python.

'''iglatinpay anslatortray

if the first two chars are consonant and a vowel, move the consonant and add 'ay' if the first two chars are both consonants, move both to the end and add 'ay' if the first letter is a vowel, add 'tay' to the end

run this script from the terminal: (for windows) py piglatin_lab5.py -from Type your sentence here like this

(or for mac) python piglatin_lab5.py -from Type your sentence here like this

''' from sys importargv import string '''the string module contains a variable called punctuation, which is all punctuation characters in one string '''

vowels = 'aeiouy' consonants = 'bcdfghjklmnpqrstvwxz'

def translateTo(startingIndex): translatedWords = '' #empty string for the result w = startingIndex #initial word index in theargv list while w <len(argv): #start at index 1 to skip the name of the program in the argv word = str(argv[w]) #chunks off the indexed word translatedWord = '' #empty string to hold the current word #strip commas, periods, etc. from the end of words if word[-1] in string.punctuation: endingPunctuation = word[-1] word = word[:len(word)-1] else: endingPunctuation = False

if len(word) > 1: #rearrange the letters in the word if word[0] in consonants and word[1] in consonants: translatedWord = word[2:]+word[:2]+'ay' elif word[0] in consonants and word[1] in vowels: translatedWord = word[1:]+word[:1]+'ay' elif word[0] in vowels: translatedWord = word+'tay' else: print("word not caught: " + word) elif len(word) == 1: translatedWord = word + 'yay' else: translatedWord = word

#if there was an ending punctuation, add it back on if endingPunctuation: translatedWord+=endingPunctuation

#add the translated word into the string of all the words, with a space at the end translatedWords += translatedWord + ' ' w += 1

#loop is done, print the translation print(translatedWords) #end of translation to piglatin

def translateFrom(startingIndex): print(argv) #do your translation here

################################## '''after defining the translations, we can check the second arg to see if a translation style is declared, else, just translate to pig latin ''' ##################################

if str(argv[1]) == '-to': print('running translation to because argv[1] is: ' + str(argv[1])) translateTo(2) elif str(argv[1]) == '-from': translateFrom(2) else: translateTo(1)

THANKS FOR THE HELP!!!!!!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!