Difference between revisions of "CSC111 Homework 5 2014"

From dftwiki3
Jump to: navigation, search
(Submission)
Line 78: Line 78:
 
<br />
 
<br />
 
Store your program in a file called '''hw5a.py''' and submit it to this URL: [http://cs.smith.edu/~dthiebaut/111b/submit5.php http://cs.smith.edu/~dthiebaut/111b/submit5.php]
 
Store your program in a file called '''hw5a.py''' and submit it to this URL: [http://cs.smith.edu/~dthiebaut/111b/submit5.php http://cs.smith.edu/~dthiebaut/111b/submit5.php]
 +
 +
<br />
 +
=Problem #2=
 +
<br />
 +
Will be provided later...
 +
<br />
 +
=Problem #3=
 +
<br />
 +
Will be provided later...
 +
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC111]][[Category:Python]][[Category:Homework]]

Revision as of 23:11, 25 February 2014

--D. Thiebaut (talk) 22:05, 25 February 2014 (EST)


This assignment is due at midnight on Thursday, 3/6/14 at midnight. You can work on it individually or work in pair. You are free to pick your partner if you decide to work in pair, in which case you should make sure that both names and 2-letter Id appear in the header of your program.





Problem #1: Random poems

This problem touches on many different Python constructs we have seen so far.

Run the following program that will remind you of the Rock, Scissors, Paper game in the way it uses a random animal.

# hw5a.py
# firstName lastName
# this program prints a random word every time it is run
#
from random import choice

# create a list of animals
farm = [ "pig", "cat", "dog", "horse", "donkey", "monkey", "snake", "fly" ]

# figure out how many animals are on the farm
animal = choice( farm )

# select the animal at that index and print its name
print "the", animal, "lives on the farm"


Create this program with Idle. Observe that every time you run it, the output is unpredictable.

Using this facility, 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. We'll see an example very shortly that 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", "Cathy", "Oprah", "Paris Hilton" ]
     adverbs   = [ "never", "sometimes", "usually", "amazingly", "voraciously" ]
     verbs     = [ "likes to sleep", "eats mice", "runs" ]
     locations = [ "around the pond", "on the sofa", "in Duckett" ]
     times     = [ "at night", "early in the morning", "every Sunday afternoon", "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 is to 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. Your program will then print that many different poems.

Here is an example of how your program should work (the user input is underlined):


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 every Sunday afternoon
The cat never runs on the sofa early in the morning
Cathy sometimes runs on the sofa at night

Use your imagination when you create the lists of words, please!

Requirements


If the user enters a number that is 0 or negative it will keep on prompting the user, and will accept only integers greater than 0.

Submission


Store your program in a file called hw5a.py and submit it to this URL: http://cs.smith.edu/~dthiebaut/111b/submit5.php


Problem #2


Will be provided later...

Problem #3


Will be provided later...