Using the function below, write a new function called pl_file that takes a strin
ID: 664223 • Letter: U
Question
Using the function below, write a new function called pl_file that takes a string with an input file name and a string with an output file name and translates all the words in the input file according to the rules, then outputs this into the output file. Catch exceptions properly for files not existing, etc.
Should be in Python.
def pl_translate(word):
t = 0
consonants = "bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"
while len(word) == 0:
word1= " You cannot translate a non-existent word! ";
for x in word:
if (x == "a") or (x == "A") or (x == "e") or (x == "E") or (x == "i") or (x == "I") or (x == "o") or (x == "O") or (x == "u") or (x == "U") or (x == "y") or (x == "Y"):
break
t = t+1
prefix = word[:t]
stem = word[t:]
if consonants not in word:
word1 =word + "yay"
else:
word1= stem + prefix + "ay"
return word1
Rules of the function:
1. If a word has no letters, don't translate it.
2. All punctuation should be preserved.
3. If the word begins with a capital letter, then the translated word should too.
4. Separate each word into two parts. The first part is called the prefix and extends from the beginning of the word up to, but not including, the first vowel. (The letter y will be considered a vowel for this.) The Rest of the word is called the stem.
5. The Pig Latin text is formed by reversing the order of the prefix and stem and adding the letters ‘ay’ to the end. For example, sandwich is composed of s + andwich which would translate to andwichsay.
6. If the word contains no consonants or starts with a vowel, let the prefix be empty and the word be the stem. The word ending should be yay instead of merely ay. For example, I would be Iyay and understood would be understoodyay.
Explanation / Answer
#definition of pl_file function
#define main method
def main():
run = True
inputf="input.txt"
output="output.txt"
print(inputF)
def pl_file(inputF,output):
print(inputF)
openFile = open(inputF, "r")
myFile = open(output, "a+")
for line in openFile.readlines():
piglatin=pl_translate(line)
print (piglatin)
myFile.write(piglatin)
myFile.write(" ")
myFile.close()
openFile.close()
def pl_translate(word):
t = 0
consonants = "bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"
while len(word) == 0:
word1= " You cannot translate a non-existent word! ";
for x in word:
if (x == "a") or (x == "A") or (x == "e") or (x == "E") or (x == "i") or (x == "I") or (x == "o") or (x == "O") or (x == "u") or (x == "U") or (x == "y") or (x == "Y"):
break
t = t+1
prefix = word[:t]
stem = word[t:]
if consonants not in word:
word1 =word + "yay"
else:
word1= stem + prefix + "ay"
return word1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.