Difference between revisions of "CSC111 Homework 3 Solutions"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- ==Problem 2== <source lang="python"> . # hw3b.py …')
 
(Problem 2)
Line 7: Line 7:
 
.
 
.
  
# hw3b.py                                                                                                                                                                                                                
+
# hw3b.py                                                                                                                            
# 111c-as                                                                                                                                                                                                                
+
# 111c-as                                                                                                                                                      
 
# Xiao Huang                 
 
# Xiao Huang                 
# (Edited by D. Thiebaut)                                                                                                                                                                                            
+
# (Edited by D. Thiebaut)                                                                                    
 
# This program prompts the user for a number of 1-line poems to print,
 
# This program prompts the user for a number of 1-line poems to print,
 
#  and generates random poems made of random subjects, random adverbs,
 
#  and generates random poems made of random subjects, random adverbs,
Line 20: Line 20:
 
     print "Welcome to the Random poems of the Day!"
 
     print "Welcome to the Random poems of the Day!"
  
     # Ask for how many poems to generate                                                                                                                                                                                
+
     # Ask for how many poems to generate                                                                          
 
     n = input("How many poems would you like me to generate for you?")
 
     n = input("How many poems would you like me to generate for you?")
  
     # Create lists lists of subjects, adverbs, verbs and so on.                                                                                                                                                          
+
     # Create lists lists of subjects, adverbs, verbs and so on.                                                        
 
     subjects  = [ "Hello Kitty", "Professor 111", "Coco Chanel", "Penny" ]
 
     subjects  = [ "Hello Kitty", "Professor 111", "Coco Chanel", "Penny" ]
 
     adverbs  = [ "mysteriously", "gently", "lazily", "violently", "doubtfully" ]
 
     adverbs  = [ "mysteriously", "gently", "lazily", "violently", "doubtfully" ]

Revision as of 23:26, 24 February 2010

--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()

.