Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. Create a function called character_count(). This function takes a filename as a parameter, and it should return the number of characters (.e., letters)
1. Create a function called character_count(). This function takes a filename as a parameter, and it should return the number of characters (.e., letters) in the file. This function is basically the same as one of the functions (word_count()) that the TA will show in the demonstration. In this function, you'll have to have an embedded loop to count the characters in each word. The function should not count the newline character. When you call this function using words.txt as input, you should receive a character count of 982 728. If you got 1 816 518, you haven't removed the newline ( ) character properly. Hint: You can use the string method, strip(), that your TA showed you in the demo, and which can be found in Chapter 9.1 of the textbook. Alternatively, you can check for the string " " and not count it; it's up to you. 2. Next, create a function called find_word (). This function takes two parameters: a filename and a number. When you call find word() with a number n, it will return the nth word in the file. For example, when called using the number 10, find word() will return aardvark. For this function, you want to keep track of which word you are at with some sort of count variable. When the count matches the number parameter, then you have arrived at the word you're looking for, so just close the file and return that word. 3. Finally, create a main() function that allows users to get the character count for a filename that they pass to it. (This means that the user will input the filename-it shouldn't be hardcoded.) Next, prompt the user for the index number they want to check for a word. Make sure that the result of each function is printed nicely using f-strings. Finally, check if the user wants to enter another index so that they can find another word. If they do, loop. If they don't, end the program. Hint: use a while loop to keep the loop running, and then use N as your sentinel value.
Step by Step Solution
★★★★★
3.42 Rating (158 Votes )
There are 3 Steps involved in it
Step: 1
Heres a Python program that implements the three functions you described def chara...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started