CSC111 Homework 3 Solutions

From dftwiki3
Revision as of 23:23, 24 February 2010 by Thiebaut (talk | contribs) (Created page with '--~~~~ ---- ==Problem 2== <source lang="python"> . # hw3b.py …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 03:23, 25 February 2010 (UTC)


Problem 2

.

# hw3b.py                                                                                                                                                                                                                 
# 111c-as                                                                                                                                                                                                                 
# Xiao Huang                
# (Edited by D. Thiebaut)                                                                                                                                                                                              
# This program prompts the user for a number of 1-line poems to print,
#  and generates random poems made of random subjects, random adverbs,
# random verbs, random locations, and random times.
#   
from random import randrange

def main():
    print "Welcome to the Random poems of the Day!"

    # Ask for how many poems to generate                                                                                                                                                                                  
    n = input("How many poems would you like me to generate for you?")

    # Create lists lists of subjects, adverbs, verbs and so on.                                                                                                                                                           
    subjects  = [ "Hello Kitty", "Professor 111", "Coco Chanel", "Penny" ]
    adverbs   = [ "mysteriously", "gently", "lazily", "violently", "doubtfully" ]
    verbs     = [ "decays", "daydreams", "eats nine burgers" ]
    locations = [ "in the bathroom", "on the moon ", "on the top of Himalayas","under the desk" ]
    times     = [ "during WWII", 
                      "before taking a walk in Central Park", 
                      "once a day","whenever possible",
                      "as soon as the end of the world comes" ]
    collection= [ subjects, adverbs, verbs, locations, times]

    # Generate n poems.
    for i in range (n):
        for item  in collection:
           # generate random index to pick random word in item
           count = len ( item ) 
           randomIndex = randrange (count)
           # print that item
           print  item[randomIndex],
        print

main()

.