Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: How to debug code. Not opening file correctly, How do i write the code to open the file and list how many times the

Question: How to debug code. Not opening file correctly, How do i write the code to open the file and list how many times the term was found in book

image text in transcribed

image text in transcribed

code :

# import texts and open files / use, try exception statement import sys

def readFile(fname): try: f = open(fname,'r',encoding="utf-8") except: print("Couldn't find the book in our library") sys.exit() # break string into lists words = f.read().split() f.close() return words

def search(text,word): return text.count(word)

main = [] print("******** Welcome to the Gutenberg Story Word Count Program*******") while(True): #option to pick between stories story = input("Choose from the list of childrens stories below: A- The Tale of Jimmy Rabbit B- The tale of Peter Rabbit C- Mother Goose E- Exit ") print()

story = story.upper() if story.lower() == "a": print("You have selected: A Tale of Jimmy Rabbit") name = 'Jimmy' fname = 'jimmy_rabbit.txt' elif story.lower() == "b": print("You have selected: A Tale of Peter Rabbit") name = 'peter' fname = 'peter_rabbit.txt' elif story.lower() == "c": print("You have selected: Mother Goose") name = 'mother' fname = 'mother_goose.txt' elif story.lower() == "e": print("Thank You for using the Word Count program, Goodbye!") sys.exit() # validate user input else: print("Invalid entry. Please try again") print() # Ask user for search term, output number of occurances while(True): term = input("Enter your search term: ") print() words = readFile(fname) print(name+" has about "+str(len(words))+" words") count = search(words,term) print("The search term "+term+" was found "+str(count)+" times") main.append([name,term,count]) print(" Your searches: ") for i in main: print("BOOK :",i[0]) print("SEARCH TERM :",i[1]) print("RESULTS :",i[2]) print() # ask if the user would like to search the story again. ch = input("Would you like to search again (y)? ") if(ch.lower()=='n'): break

instructions from teacher

image text in transcribedimage text in transcribed

Enter your search term: mother mother has about 1209 words The search term mother was found 0 times Your searches: BOOK : mother SEARCH TERM : goose RESULTS : 0 BOOK : mother SEARCH TERM : mother RESULTS : 0 Would you like to search again (y)? y Enter your search term: went mother has about 1209 words The search term went was found 0 times Your searches: BOOK : mother SEARCH TERM : goose RESULTS : 0 BOOK : mother SEARCH TERM : mother RESULTS : 0 BOOK : mother SEARCH TERM : went RESULTS : 0 Would you like to search again (y) ? n Choose from the list of childrens stories below: A- The Tale of Jimmy Rabbit B- The tale of Peter Rabbit C- Mother Goose E- Exit Welcome to the Gutenberg Story Word Count Program Choose from the list of childrens stories below: A- The Tale of Jimmy Rabbit B- The tale of Peter Rabbit C- Mother Goose E- Exit a You have selected: A Tale of Jimmy Rabbit Enter your search term: rabbit Jimmy has about 13204 words The search term rabbit was found 1 times Your searches: BOOK : Jimmy SEARCH TERM : rabbit RESULTS : 1 Would you like to search again (y)?y Enter your search term: jimmy Jimmy has about 13204 words The search term jimmy was found 0 times Your searches: BOOK : Jimmy SEARCH TERM : rabbit RESULTS : 1 BOOK : Jimmy SEARCH TERM : jimmy RESULTS : 0 Would you like to search again (y)? Gutenberg Story Word Counter Program Please choose from one of the stories below: A - Frankenstein (1818) B - The Strange Case of Dr. Jekyll and Mr. Hyde (1886) C - The Turn of the Screw (1898) E - Exit Program Your choice: A Great, we will work with Frankenstein. Enter your search term: strange Frankenstein has about 74975 words. The search term strange was found 28 times in Frankenstein. Your searches: Would you like to search again (y)?y Enter your search term: child Frankenstein has about 74975 words. The search term child was found 18 times in Frankenstein. Your searches: Would you like to search again (y)?n Please choose from one of the stories below: A - Frankenstein (1818) B - The Strange Case of Dr. Jekyll and Mr. Hyde (1886) C - The Turn of the Screw (1898) E - Exit Program Your choice: E Thank you! Goodbye. Step Two: Create Program that meets these requirements - Remember to comment your code - include your name, date, class, and assignment title. - Provide comments throughout your program to document your code process. - Create a greeting/title to your program display output. - Present your user with a list of 3 stories that they can choose to work with. Give them an option to exit as well with a goodbye message. - Validate the user input in case they don't input a valid number or letter. Have them try again if the user input is incorrect. - Once a story is selected, use a try, exception statement to check that you can open the file. Output - "Sorry that story cannot be found" or something like that if the file cannot be opened. - Store the text of the file to a string datatype (see page 203 for an example). - Then break the string up into a list. (We haven't learned this yet, but here is a simple example of how to convert a string to a list). In this example, storyStr is the text from the read in file (your text will be much longer). Then we create a variable storyWords which is a saved list of words (separated by spaces) created by using the .split() string function. See my REPL example B for a demonstration of this. storyStr = 'Mowgli was far and far through the forest, running hard, and his heart was hot in him. He came to he cave as the evening mist rose, and drew breath, and looked down the valley. The cubs were out, but Mother Wolf, at the back of the cav es, knew by his breathing that something was troubling her frog.' storyWords=storyStr.split( - Ask for user input for a word or phrase to search for. - Search for the word or phrase in the list. Output how many occurrences of the search term were found. Also output the total number of words in the file. - Save the search term and the search count to a list. Output the list for the search results for this story. - Ask if the user would like to search the story again. - If yes, complete the process. If no, display the original list of stories again with an option to exit the program completely. Example program output (feel free to be creative with your output) Note: I googled for the publication year

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions