Assignment 6 -- Mad Libs

advertisement
Assignment 6 – Mad Libs in Python
Due: Tuesday, November 1st
10 points
Dean Zeller
CS10051
Fall, 2007
Objective
The student will create interactive Mad Libs activities in Python.
Readings
Python Library Reference (string methods) at http://docs.python.org/lib/string-methods.html
Background: String Formatting
This assignment is a change in direction from the previous assignments, in that graphics are not a component.
Interaction with the user can be completed using a text interface. You will be using strings to implement the
common educational activity, Mad Libs. Strings may use either ‘single quotes’ or “double quotes.”
Instructions
Follow the design requirements below to create at least two mad libs stories in Python, similar to the template
below. To encourage reusability, follow these rules. See the web pages in for activity 6 for ideas.
Rule 1. The code for the mad lib activity must be completely within a procedure, following the template
below. A programmer should be able to execute the activity by calling the function.
Rule 2. Each procedure must have documentation clearly indicating the title, author(s), date completed, date
modified (if necessary), and a brief description. It must look exactly like the template, including
the pound signs (#) and blank lines. This level of documentation is necessary to allow others to
read and understand your work. Use comments throughout the code to describe what the code is
doing.
Rule 3. The optional parameter of textwidth should be available for the programmer, with a default
value of 50.
Rule 4. Input from the user is gained through the raw_input command.
Rule 5. Name your entry variables in the following manner:
nounHare
Part of
speech
Entry in
original
story
verbRan
Part of
speech
Entry in
original
story
Rule 6. Use Python’s print command to print the story with the entry variables in place of the original
words. Experiment with IDLE’s interactive environment to understand the difference. The
following is a guide on the use the comma and plus sign in a print statement:
Use a comma (,) to separate strings (adds a space).
print "hello", "there"
print "So the rabbit said", greeting
Use a plus sign (+) to concatenate strings (does not add a space)
print "hello" + "there"
print "So the rabbit said " + greeting
Rule 7. This assignment is an exercise in text formatting. The Python string formatting commands are
available to help space your text correctly. Sometimes getting text to look correct on the screen can
be challenging.
To center a string within a width of 50 characters:
print "Hello there".center(50)
print ("By "+firstName+" "+lastName).center(50)
To right justify a string within a width of 50 characters:
print "Hello there".rjust(50)
print ("By "+firstName+" "+lastName).rjust(50)
To capitalize the first letter of a string:
print firstName.capitalize()
Turning in your assignment
 Make a printout of your code.
 Test each mad lib story once, with reasonable (but funny) entries. Make a printout of your tests.
Grading
You will be graded on the following criteria:
Format
Correctly following the design specifications
Quantity
Size and complexity of the stories implemented
Creativity
Imagination and originality in the stories
Extra Credit
 Implement additional stories
madlibs.py
###########################################################################
#
#
#
Mad Lib functions
#
#
by Dean Zeller
#
#
#
#
The following is a library of mad lib procedures.
#
#
#
###########################################################################
###########################################################################
#
Tortoise and Hare Mad Lib
#
#
Programmed by Dean Zeller (10-20-07)
#
#
Source: Mad Libs web site
#
#
This is the common fable of the race between the tortoise and the
#
#
hare.
#
###########################################################################
def tortoise_n_hareML(textwidth=50):
half = textwidth / 2
# print title
print
print
print "Welcome to Mad Libs(r)".center(textwidth)
print
print "Please input the following entries:"
# input entries from user
nounHare
= raw_input("noun (singular) => ".rjust(half))
verbBoasting = raw_input("verb (ending in ing) => ".rjust(half))
nounAnimals
= raw_input("noun (plural) => ".rjust(half))
nounTortoise = raw_input("noun (singular) => ".rjust(half))
verbChallenge = raw_input("verb (present tense) => ".rjust(half))
nounJoke
= raw_input("noun (singular) => ".rjust(half))
verbStopped
= raw_input("verb (past tense) => ".rjust(half))
nounNap
= raw_input("noun (singular) => ".rjust(half))
# print story with user entries
print
print "Thank you. Here is your Mad Lib!"
print
print ("The "+nounTortoise.capitalize()+" and the "+nounHare.capitalize()).center(textwidth)
print "(from The Tortoise and the Hare)".center(textwidth)
print
print "
The",nounHare,"was once",verbBoasting,"of his speed before"
print "the other",nounAnimals+". 'I have never been beaten,' said he,"
print "'when I put furth my full speed. I",verbChallenge,"any one here"
print "to race with me. The",nounTortoise,"said quietly, 'I accept your"
print verbChallenge+".'"
print "
'That is a good",nounJoke+",' said the ",nounHare+"; 'I could"
print "dance round you all the way.'"
print "
'Keep your",verbBoasting,"till you've won,' answered the"
print nounTortoise+". 'Shall we race?'"
print "
So a course was fixed and a start was made. The",nounHare
print "darted almost out of sight at once, but soon",verbStopped,"and,"
print "to show his contempt for the",nounTortoise, "lay down to have a"
print nounNap+". The",nounTortoise,"plodded on and plodded on, and when"
print "the",nounHare,"awoke from his nap, he saw the",nounTortoise,"just"
print "near the winning-post and could not run up in time to "
print "save the race."
print "
Then the",nounTortoise,"said 'Slow but stead progress wins the race.'"
print
print "The End".rjust(textwidth)
###########################################################################
#
Old McDonald Mad Lib
#
#
Programmed by Dean Zeller (10-20-07)
#
#
Source: Mad Libs web site
#
#
This is the children's song Old McDonald Had a Farm.
This Mad Lib
#
#
is unique in that the entries are used more than once. This is not
#
#
possible in a traditional mad lib.
#
###########################################################################
def old_mcdonaldML(textwidth = 50):
half = textwidth / 2
# print title
print
print
print "Welcome to Mad Libs(r)".center(textwidth)
print
print "Please input the following entries:"
# input entries from user
adjOld
= raw_input("adjective => ".rjust(half))
nounFarm
= raw_input("noun (singular) => ".rjust(half))
nounCow
= raw_input("noun (singular) => ".rjust(half))
expMoo
= raw_input("explamation => ".rjust(half))
nounChicken = raw_input("noun (singular) => ".rjust(half))
expBawk
= raw_input("explamation => ".rjust(half))
nounPig
= raw_input("noun (singular) => ".rjust(half))
expOink
= raw_input("explamation => ".rjust(half))
# print story with user entries
print
print "Thank you. Here is your Mad Lib!"
print
print (adjOld.capitalize()+" McDonald").center(textwidth)
print "(from Old McDonald)".center(textwidth)
print
print adjOld.capitalize(),"McDonald had a",nounFarm,"E - I - E - I - O"
print "And on his",nounFarm,"he had a",nounCow,"E - I - E - I - O"
print "With a '"+expMoo+"-"+expMoo+"' here and a '"+expMoo+"-"+expMoo+"' there."
print "Here a '"+expMoo+"' there a '"+expMoo+"'"
print "Everywhere a '"+expMoo+"-"+expMoo+"'"
print adjOld.capitalize(),"McDonald had a",nounFarm,"E - I - E - I - O"
print
print adjOld.capitalize(),"McDonald had a",nounFarm,"E - I - E - I - O"
print "And on his",nounFarm,"he had a",nounChicken,"E - I - E - I - O"
print "With a '"+expBawk+"-"+expBawk+"' here and a '"+expBawk+"-"+expBawk+"' there."
print "Here a '"+expBawk+"' there a '"+expBawk+"'"
print "Everywhere a '"+expBawk+"-"+expBawk+"'"
print "With a '"+expMoo+"-"+expMoo+"' here and a '"+expMoo+"-"+expMoo+"' there."
print "Here a '"+expMoo+"' there a '"+expMoo+"'"
print "Everywhere a '"+expMoo+"-"+expMoo+"'"
print adjOld.capitalize(),"McDonald had a",nounFarm,"E - I - E - I - O"
print
print adjOld.capitalize(),"McDonald had a",nounFarm,"E - I - E - I - O"
print "And on his",nounFarm,"he had a",nounPig,"E - I - E - I - O"
print "With a '"+expOink+"-"+expOink+"' here and a '"+expOink+"-"+expOink+"' there."
print "Here a '"+expOink+"' there a '"+expOink+"'"
print "Everywhere a '"+expOink+"-"+expOink+"'"
print "With a '"+expBawk+"-"+expBawk+"' here and a '"+expBawk+"-"+expBawk+"' there."
print "Here a '"+expBawk+"' there a '"+expBawk+"'"
print "Everywhere a '"+expBawk+"-"+expBawk+"'"
print "With a '"+expMoo+"-"+expMoo+"' here and a '"+expMoo+"-"+expMoo+"' there."
print "Here a '"+expMoo+"' there a '"+expMoo+"'"
print "Everywhere a '"+expMoo+"-"+expMoo+"'"
print adjOld.capitalize(),"McDonald had a",nounFarm,"E - I - E - I - O"
# Call the functions
old_mcdonaldML()
tortoise_n_hareML()
Download