Starting from:

$29

Assignment 3: files, strings and functions

Assignment 3: files, strings and functions
Files to submit: madlibs.py and tableprinter.py
1. Madlibs
Specification: You are to put the code for the function below into the file madlibs.py
mad_libs(inputname,outputname) where the parameters are file paths. The
function reads in the contents of the file named inputname and prompts the user for
words to replace each occurrence of the words ADJECTIVE, NOUN, ADVERB, or
VERB. The function then does the replacements, prints the modified string to the
screen and to the file with name outputname.
For example, the contents of the input text file may look like this:
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was
unaffected by these events.
If so, the output to the screen should look like this, with user input underlined
Enter an adjective: silly
Enter a noun: chandelier
Enter a verb: screamed
Enter a noun: pickup truck
The silly panda walked to the chandelier and then screamed. A
nearby pickup truck was unaffected by these events.
This string would also be written to a file with name outputname
2. Table Printer
Specification: You are to put the code for the function printTable() into the file
tableprinter.py
The function printTable(L) that takes a list L of lists of strings and displays it in a wellorganized table with each column right-justified. Assume that all the inner lists will contain
the same number of strings.
Given
tableData = [['apples', 'oranges', 'cherries', 'banana'],
 ['Alice', 'Bob', 'Carol', 'David'],
 ['dogs', 'cats', 'moose', 'goose']]
the function should print the following:
 apples Alice dogs
oranges Bob cats
cherries Carol moose
 banana David goose
Hint: Your code will first have to find the longest string in each of the
inner lists so that the whole column can be wide enough to fit all the strings.
You can store the maximum width of each column as a list of integers. These widths will
be used with the string method rjust to attain the desired alignment

More products