CSC111 Homework 3 2011
Problem #1: Random poems
It is sometimes necessary to select random items in a list. Generating random numbers is an easy task, and is can be done by using the randrange() function that is available in a standard Python program called random.py. Because this special program is part of the language, and available with all versions of python (for Linux, Windows, or the Mac), you do not need to physically get a copy of it as you did in the previous assignment. It is always there on the system, installed as part of the python language. All you have to do in your program is state that you want to use the function randrange() that is part of the random program.
Here is an example of how this works:
.
# hw3a.py
# 111a-xx
# firstName lastName
# this program prints a random word every time it is run
#
from random import randrange
def main():
# create a list of animals
farm = [ "pig", "cat", "dog", "horse", "donkey", "monkey", "snake", "fly" ]
# figure out how many animals are on the farm
animalCount = len( farm )
# pick a number at random that is between 0 (included) and
# the number of animals (excluded)
randomIndex = randrange( animalCount )
# select the animal at that index and print its name
print( "the", farm[ randomIndex ], "lives on the farm" )
main()
.
Create this program in your 111a-xx account on Beowulf (or cut and paste it from this Web page). Observe that every time you run it, the output is unpredictable.
Using this new possibility, write a program that creates random poems. In our case, a poem will be just one line long, and will contain a subject, an adverb, a verb, a location, and a time. An example will clarify this idea.
Assume that we have a list of subjects, a list of adverbs, a list of verbs, a list of locations, and a list of times, as follows:
.
subjects = [ "The cat", "Mrs Christ", "Our CS prof", "Paris Hilton" ]
adverbs = [ "never", "sometimes", "usually", "amazingly", "voraciously" ]
verbs = [ "likes to sleep", "eats mice", "runs" ]
locations = [ "around the pond", "on the sofa", "in Duckett House" ]
times = [ "at night", "early in the morning", "every Sunday afernoon", "whenever possible" ]
.
If we pick a random word from each list and "glue" all the words together, we end up with a one-line poem. For example Paris Hilton amazingly eats mice in Duckett House every Sunday afternoon. Your assignment: write a program that declares a different series of lists (lists should not have the same number of words), and asks the user how many one-line poems she would like to see. In response the program will print that many different poems.
Here is an example of how your program should work:
Welcome to the Random Poems of the Day! How many poems would you like me to generate for you? 3 Here are your 3 poems: Paris Hilton amazingly eats mice in Duckett House every Sunday afternoon The cat never runs on the sofa early in the morning Mrs Christ sometimes runs on the sofa at night
Use your imagination when you create the lists of words, please!
Store your program in a file called hw3a.py and submit it as follows:
submit hw3 hw3a.py