Using the function below, write a new function called pl_file that takes a strin
ID: 664213 • 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
Explanation / Answer
def pl_file(infile_name,outfile_name):
word1=''
txt = open(infile_name)
filelist = txt.read()
t = 0
consonants = "bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"
while len(filelist) == 0:
word1= " You cannot translate a non-existent word! ";
for x in filelist:
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 = filelist[:t]
stem = filelist[t:]
if consonants not in filelist:
word1 =filelist + "yay"
else:
word1= stem + prefix + "ay"
print "Opening the file..."
target = open(outfile_name, 'w')
target.write(word1);
target.close()
return word1;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.