Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3.6 Question 12 (20 points) Write a function named wordCount that counts

ID: 3728736 • Letter: P

Question

Python 3.6

Question 12 (20 points) Write a function named wordCount that counts how many words there are in each line of an input file and writes that count to a corresponding line of an output file. The input file already exists when wordCount is called, wordCount creates the output file Input. The function wordCount takes two parameters: inFile, a string that Is the name of a text file that Is to be read and analyzed. The file that inFile refers to contains only upper and lower case letters and white space (no punctuation marks or other special characters) i. ii. outFile, a string that is the name of the file to which wordCount writes its output. The input file is in the current working directory and you should create the output file to that directory. Output. For each line of inFile, wordCount should write a corresponding line to outFile containing a single integer: the number of words on the line If the content of the file catInTheHat.txt is below The sun did not shine It was too wet to plav So we sat in the house All that cold cold wet day I sat there with Sallv We sat there we two And I said How I wish We had somethina to do the function call wordCount( 'catInTheHat.txt', 'catInTheHatOut.txt') should create a file named catInTheHatOut.txt with this content: 5 6 6 6 5 5 6 5

Explanation / Answer

def wordCount(inputFile, outputFile): try: f = open(inputFile) out = open(outputFile, "w") for line in f: out.write(len(line.split())) out.write(" ") f.close() out.close() except Exception: print("File not found")