MC logo
Python Assignment 3
[^] Python Home
[Python Assignment 1] [Python Assignment 2] [Python Assignment 3] [Python Assignment 4] [Python Assignment 5]
[A3 Example Input]

Another Word For It

Feb 15
Mar 5
90 pts

Write a random word-replacement program, similar to the mad-lib game which children enjoy. The input consists of a series of word lists, followed by some text into which words are substituted from the lists at random.

Input Format

The input has two sections. The first is a series of word lists, one per line, and the second is some text to be output with some words replaced. The two sections are separated by a line containing just a dash.

The first section is a series of one or more lists of words, each given on a single line as a series of blank-separated words. The words may be separated by any number of blanks. Words may not contain blanks.

This section is terminated by a line consisting of only a -.

The second section is a series of lines which are are copied to the output, some of which after replacement by a random word from the substitution list. Any construct consisting of a number sign (#) followed by at least one digit is to be replaced with a word from the corresponding list. Nothing else is to be replaced. The number following the number sign refers to one of the lists from the first section, numbered starting from zero up to the number of lists less one. This item should be replaced by one chosen randomly from the list indicated by the number. If the number is out of range, substitute the word [error] instead of choosing from a list.

For instance:

Mike Bill Susie Fran Alex
  ran walked    drove
store house boat   dock 'fridge
duck dog filbert computer generator
  red blue avian international insane emancipated arbitrary
wings ears feet fingers eyes nodules elbows
#0 #1 to the #4 #2. Inside, there was a large #3
with #4 #5.


The program should read and store the replacement section without producing any output. It should then copy the lines in the second section to the output with replacement. When choosing replacement words, a word should be chosen from the correct list with equal probability for each word. In general, each run of the program will produce different output. Some possibilities from the above input are:

Bill walked to the insane house. Inside, there was a large generator
with red ears.
Susie ran to the blue dock. Inside, there was a large dog
with emancipated wings.

Note: You must read from standard input. Your program may not open a file.

Picky Details

Any # in the second section starts a replacement. The # should be followed by a number. If it is not, you may do whatever you wish: ignore it, use list 0, write an error message, or something else. Otherwise, convert the digits to an integer so you can use them to subscript your lists, remove the # and the digits, and substitute with a randomly-chosen word.

Do not substitute words in the substitution lists. That is, #3 specifies a substitution when it appears in the second section. If it appears in a word list in the first section, it's just a word that can be printed if the random process selects it.


You probably want to read the input one line at a time. The easiest way is to use two successive loops, one for the first section and one for the second. The first loop ends after reading the - line, and the second ends at the end of the file. Store the replacement words in a list of lists. Create an empty list at the start of your program. In the first loop, read each line, then split the line to make an array of strings. Use append or something similar to add this new list to the end of the main list. When the first loop finishes, you will have a list of all your replacement lists.

In the second loop, read each line and search for #. When you find one, find the digits that follow it. Convert the digits to an integer, and use it to find the replacement list. Use Python's Random module to choose a word at random from the selected list.

>>> import random
>>> random.randint(1,10)
>>> random.randint(1,10)
>>> list = ['this','is','some','words']
>>> list[random.randint(0,len(list)-1)]
>>> list[random.randint(0,len(list)-1)]
>>> list[random.randint(0,len(list)-1)]
>>> list[random.randint(0,len(list)-1)]

Piping In The Bits

When running a program from the command line, the program reads standard input from the console by default. You can change this by specifying the input file using the < operator. For instance
[tom@laptop asst]$ python asst3.py < mad2in.txt
Fran walked to the international store. Inside, there was a large dog
with emancipated nodules. 
[tom@laptop asst]$ 
This also works fine from the Windows command window.


When your program works, is well-commented, and nicely indented, submit over the web here.
<<Python Assignment 2 Python Assignment 4>>