Question

Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: val...

Sample program run - Needs to be coded in python

-Enter the text that you want to search for, Or DONE when finished: valiant paris The valiant Paris seeks for his love.

-Enter the text that you want to search for, or DONE when finished: Romeo and Juliet

Enter Romeo and Juliet aloft, at the WIndow

-- Enter the text that you want to search for, DONE when finished: DONE  

# copy the following two lines into any
# program that you write that will work with files
import sys, os
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

def openTheBook(bookname):
   """ Function that opens a book. The book is returned as as string.

   Parameters: The name of the book, as a string
  
   Returns: The text of the book, as a string
  
   Assumptions: The file containing the book is in the same folder
   as this Python program.
   """
   # open the file. Again, use this code. The variable 'bookname' has the name
   # of the file that contains the book
  
   textFile = open(os.path.join(__location__,bookname), 'r')
  
   # the book is now open. Read the book into a list, then close the book.
  
   bookList = [] # empty list
  
   for line in textFile:
       bookList.append(line) # add the line to the bookList list
  
   textFile.close()
  
   # return the book's contents as a list to the calling function
   return bookList
  
def searchTheBook(bookname):
   """ Function that processes the contents of a book.

   Parameters: The name of the book to be processed
  
   Returns: nothing. All information is displayed on the screen.
  
   Uses: The openTheBook function to get the contents of the book.
   """
   # get the book contents, as a list
   book = openTheBook(bookname)
  
"""
Add your code here
"""

  
  
def main():
   """ Call the function to work with the text file
   Parameters: none
  
   Returns: nothing
   """
   filename = 'RomeoAndJuliet.txt'
   searchTheBook(filename)
  
# run the program  
main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: replace searchTheBook() with below given code and modify the main() function with below given code

import sys, os
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

def openTheBook(bookname):
   """ Function that opens a book. The book is returned as as string.
   Parameters: The name of the book, as a string
   Returns: The text of the book, as a string
   Assumptions: The file containing the book is in the same folder
   as this Python program.
   """
   # open the file. Again, use this code. The variable 'bookname' has the name
   # of the file that contains the book
   textFile = open(os.path.join(__location__,bookname), 'r')
   # the book is now open. Read the book into a list, then close the book.
   bookList = [] # empty list

   for line in textFile:
       #print line
       bookList.append(line) # add the line to the bookList list

   textFile.close()

   # return the book's contents as a list to the calling function
   return bookList

def searchTheBook(bookname):
   """ Function that processes the contents of a book.
   Parameters: The name of the book to be processed
   Returns: nothing. All information is displayed on the screen.
   Uses: The openTheBook function to get the contents of the book.
   """
   # get the book contents, as a list
   book = openTheBook(bookname)
   #added code
   for line in book:
       print line


def main():
   """ Call the function to work with the text file
   Parameters: none
   Returns: nothing
   """
   while(True):
       filename=raw_input('Enter the text that you want to search for, or DONE when finished:')
       if filename == 'DONE':
           break
       else:
           #filename = 'presidents.txt'
           searchTheBook(filename)
  
# run the program
main()

output:

Enter the text that you want to search for, or DONE when finished: presidents.txt Abe Lincoln honest 6 James Munore Doctrine

Add a comment
Know the answer?
Add Answer to:
Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: val...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm a bit confused on how to get this program to run right. Here are the...

    I'm a bit confused on how to get this program to run right. Here are the directions: Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters ‘  ‘This line has extra space characters’ Function name: reduceWhitespace Number of parameters: one string line Return value: one string line The main file should handle the...

  • Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is...

    Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...

  • Must be done in python and using linux mint Write a program to create a text...

    Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...

  • Your task is to process a file containing the text of a book available as a...

    Your task is to process a file containing the text of a book available as a file as follows: A function GetGoing(filename) that will take a file name as a parameter. The function will read the contents of the file into a string. Then it prints the number of characters and the number of words in the file. The function also returns the content of the file as a Python list of words in the text file. A function FindMatches(keywordlist,...

  • Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from...

    Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt, verbs. txt, articles.txt, and prepositions.txt. (HINT: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list...

  • PYTHON The provided code in the ATM program is incomplete. Complete the run method of the...

    PYTHON The provided code in the ATM program is incomplete. Complete the run method of the ATM class. The program should display a message that the police will be called after a user has had three successive failures. The program should also shut down the bank when this happens. [comment]: <> (The ATM program allows a user an indefinite number of attempts to log in. Fix the program so that it displays a message that the police will be called...

  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • Using Python, if you could help me with the code # Create a modified version of...

    Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT