Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Let's write a program that can handle any Madlib. To do that we will read the Madlib from a file. The MadLib will contain words

Let's write a program that can handle any Madlib. To do that we will read the Madlib from a file. The MadLib will contain words like _name_ or _a_place_ which identify the blanks that must be filled in. Your program should prompt the user for a substitution for each blank in the file before printing the resulting Madlib. Some code is provided to get you started. There are also comments to guide you. Sample Input There are four sample files for you to test your program. You can download these files below. madlib1 madlib2 madlib3 madlib4 The contents of the madlib4 file are _name_ is too cool for _noun_ class. Instead, they will be attending _an_event_ . Note that there are spaces surrounding each _blank_ in the input file to simplify parsing them. The same input for the program includes the filename, followed by the values for the _blanks_. Here is some sample input for madlib4. madlib4 Jessie Python concert Sample Output The output for the madlib4 file should look like this when given the sample input above. MadLib filename? name? noun? an event? Jessie is too cool for Python class. Instead, they will be attending concert . Your solution should contain the following functions: 1. madlib_word(word) - An implementation of this function is already given. This function replaces the blanks in madlib with given word. 2. madlib_line(text) - This function should make a call to madlib_word function for building a sentence/line constituting the words. 3. madlib_file(filename) - This function should make a call to madlib_line to build each sentence of the given madlib and should write each line built in above step to the given madlib filename 4. madlib_print(madlib) - Should print a completed madlib from the given madlib file.

image text in transcribed

1 # MadLibs from a file 3 def madlib_word (word): "" "Returns a substitution for a fill-in-the-blank or the original word."" if word[0] == "_" == word[-1]: return input (word[1:-1].replace("_", " ")+'? ') else: return word 11 def madlib_line (text): """Returns a string containing the line with the fill-in-the-blanks substituted."" words = text.split() # try printing this to see what it does # process all the words in the line and return a string - hint use madlib_word return def madlib_file(filename): "Reads the madlib file and returns the completed madlib as a list of strings.""" # process all the lines in the file and return a list of strings - hint use madlib_line return def madlib_print(madlib): ""Prints a completed MadLib, line by line." # print the strings in the list return def main(): """Completes the user supplied madlib." filename = input("Madlib filename? ") madlib_print(madlib_file(filename)) 36 # Do not change the following lines or the tests will fail. 37 if __name__ == '__main__': 38 main()

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

Step: 3

blur-text-image

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

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

b. Where did they come from?

Answered: 1 week ago