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
  • 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...

  • 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...

  • PYTHON PROGRAMMING: I have this program right now where it allows users to choose from a...

    PYTHON PROGRAMMING: I have this program right now where it allows users to choose from a category(pulling from the file). Then it will print the University or people from that text file. What I want to do next on my code is for users to search for a specific string from that file and it will display both the University and People that have that matching string. It can be the whole word or part of the string from that...

  • 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...

  • 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...

  • Python coding exercise: please include comments Goal #1: import financial data given into your program provided...

    Python coding exercise: please include comments Goal #1: import financial data given into your program provided to you as a CSV formatted text file Use the following data for testing (the following is only a sample of the data; there are over 4000 rows of data): *Note: The data you will read in is linear by date (but, non-contiguous due to holidays and weekends,) reflecting a timeline of stock performance in chronological order; however your program should run through the...

  • Python3, write a program, specific information is in the graphs, I got 7 marks out of...

    Python3, write a program, specific information is in the graphs, I got 7 marks out of 10, you may change my codes to let it gives correct output,restrications are also on the graph, for loop, in key word, enumerate,zip,slices, with key word can't be used. All the information is on the graph, is_shakespeare_play(line) may be a function given that can check whether is Shakespeare play Question 2- 10 marks Write the function keep titles short (filename, max charactars) which takes...

  • In this assignment, you will explore more on text analysis and an elementary version of sentiment...

    In this assignment, you will explore more on text analysis and an elementary version of sentiment analysis. Sentiment analysis is the process of using a computer program to identify and categorise opinions in a piece of text in order to determine the writer’s attitude towards a particular topic (e.g., news, product, service etc.). The sentiment can be expressed as positive, negative or neutral. Create a Python file called a5.py that will perform text analysis on some text files. You can...

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