Difference between revisions of "NicelyDocumentedProgram.py"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ <source lang="python"> # hw3b.py # Jane Doe 111a-as # ( edited by D. Thiebaut ) # this program prints a random poem as many times as # the user chooses. # # each poem is…')
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
--[[User:Thiebaut|D. Thiebaut]] 14:41, 29 January 2010 (UTC)
+
--[[User:Thiebaut|D. Thiebaut]] 07:27, 8 September 2011 (EDT)
 +
----
  
 
<source lang="python">
 
<source lang="python">
Line 12: Line 13:
 
# The program keeps a collection of subjects, adverbs, verbs,
 
# The program keeps a collection of subjects, adverbs, verbs,
 
# locations and times.
 
# locations and times.
 +
#
 +
# (NOTE: This program is for Python Version 2.  Won't work with
 +
#              Python V. 3 and above.)
 
#
 
#
 
from random import randrange
 
from random import randrange
  
#--- ask user for number of poems ---
+
def main():
print "Welcome to Random Poems of the Day!"
+
  #--- ask user for number of poems ---
noPoems = input("How many poems would you like me to write? ")
+
  print "Welcome to Random Poems of the Day!"
print
+
  noPoems = input("How many poems would you like me to write? ")
 +
  print
  
#--- create lists of poem elements ---
+
  #--- create lists of poem elements ---
subjects  = ['Kep', 'Ford Prefect', 'Albus Dumbledore', 'Madeline LEngle']
+
  subjects  = ['Kep', 'Ford Prefect', 'Albus Dumbledore', 'Madeline LEngle']
adverbs    = ['quietly', 'vehemently', 'surprisingly', 'naturally', 'explosively']
+
  adverbs    = ['quietly', 'vehemently', 'surprisingly', 'naturally', 'explosively']
verbs      = ['exfoliates', 'defenestrates', 'drinks peppermint tea']
+
  verbs      = ['exfoliates', 'defenestrates', 'drinks peppermint tea']
locations  = ['in Chicago', 'by the statue of the one-eyed witch',\
+
  locations  = ['in Chicago', 'by the statue of the one-eyed witch',\
              'under my desk']
+
                  'under my desk']
times      = ['around teatime', 'before breakfast', 'during quiet hours',\
+
  times      = ['around teatime', 'before breakfast', 'during quiet hours',\
              'while watching House']
+
                'while watching House']
collection = [ subjects, adverbs, verbs, locations, times ]
+
  collection = [ subjects, adverbs, verbs, locations, times ]
  
#--- print all the poems, each one randomly picked ---
+
  #--- print all the poems, each one randomly picked ---
for i in range( noPoems ):
+
  for i in range( noPoems ):
  
  #--- pick a group of words ---
+
      #--- pick a group of words ---
  for group in collection:
+
      for group in collection:
      count = len( group )            # total # words in group
+
        count = len( group )            # total # words in group
      randomIndex = randrange( count ) # random index less than count
+
        randomIndex = randrange( count ) # random index less than count
      print group[ randomIndex ],      # just print random word at that index
+
        print group[ randomIndex ],      # just print random word at that index
  print
+
      print
  print
+
      print
 
    
 
    
 
+
main()
 
</source>
 
</source>
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC111]][[Category:Python]]

Latest revision as of 10:21, 27 January 2014

--D. Thiebaut 07:27, 8 September 2011 (EDT)


# hw3b.py
# Jane Doe  111a-as
# ( edited by D. Thiebaut )
# this program prints a random poem as many times as
# the user chooses.
#
# each poem is made from a random subject, random adverb,
# random verb, random location, and random time.
# The program keeps a collection of subjects, adverbs, verbs,
# locations and times.
#
# (NOTE: This program is for Python Version 2.  Won't work with
#               Python V. 3 and above.)
#
from random import randrange

def main():
   #--- ask user for number of poems ---
   print "Welcome to Random Poems of the Day!"
   noPoems = input("How many poems would you like me to write? ")
   print

   #--- create lists of poem elements ---
   subjects   = ['Kep', 'Ford Prefect', 'Albus Dumbledore', 'Madeline LEngle']
   adverbs    = ['quietly', 'vehemently', 'surprisingly', 'naturally', 'explosively']
   verbs      = ['exfoliates', 'defenestrates', 'drinks peppermint tea']
   locations  = ['in Chicago', 'by the statue of the one-eyed witch',\
                  'under my desk']
   times      = ['around teatime', 'before breakfast', 'during quiet hours',\
                 'while watching House']
   collection = [ subjects, adverbs, verbs, locations, times ]

   #--- print all the poems, each one randomly picked ---
   for i in range( noPoems ):

      #--- pick a group of words ---
      for group in collection:
         count = len( group )             # total # words in group
         randomIndex = randrange( count ) # random index less than count
         print group[ randomIndex ],      # just print random word at that index
      print
      print
   
main()