Question: Write a function called countWords() that accepts a filename and an integer representing the number of lines. e.g. countWords('raven.txt', 20). In this case, your function

Write a function called countWords() that accepts a filename and an integer representing the number of lines. e.g. countWords('raven.txt', 20).

In this case, your function should print the number of words in the first 20 lines of raven.txt

There should be a tab between the colon and the number of words. You must use a format string in your code.

Example:

countWords('raven.txt',10)

would output:

Line #1: 11 words

Line #2: 10 wordsLine #3: 10 wordsLine #4: 11 wordsLine #5: 11 wordsLine #6: 5 wordsLine #7: 0 wordsLine #8: 10 wordsLine #9: 11 wordsLine #10: 12 words

To keep things a bit simpler, you may assume that the second argument to the function will not ask for too many lines. For example, we will assume the user does not invoke: countWords('raven.txt',400000)

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 Programming Questions!