Difference between revisions of "CSC111 A Form Generator Program"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- ==Exercise== Write a Python program that displays forms around names. The forms can be used to record information about candidates for a particular position, for ...")
 
(Solution Program)
 
(6 intermediate revisions by the same user not shown)
Line 28: Line 28:
 
  Height of blank box? <u>4</u>
 
  Height of blank box? <u>4</u>
 
  How many names? <u>3</u>
 
  How many names? <u>3</u>
  > Alice in Wonderland
+
  > <u>Alice </U>
  > Tintin et Milou
+
  > <u>Tintin et Milou</u>
  > Bugs Bunny
+
  > <u>Bugs Bunny</u>
 
   
 
   
 
   
 
   
Line 38: Line 38:
 
   
 
   
 
  +--------------------------------------+
 
  +--------------------------------------+
  |Alice in Wonderland| Date:            |
+
  |Alice             | Date:            |
 
  |                  +-------------------+
 
  |                  +-------------------+
 
  |                                      |  
 
  |                                      |  
Line 70: Line 70:
 
  +--------------------------------------+
 
  +--------------------------------------+
 
   
 
   
 +
 +
 +
>>>
 +
 +
==Solution Program==
 +
 +
<source lang="python">
 +
# formgen.py
 +
# D. Thiebaut
 +
# generates a form with names of candidates
 +
#
 +
# the program first prompts the user
 +
# for the width of the form, and the number
 +
# of names for which forms will be generated
 +
# Then the user enters the names and the program
 +
# outputs the forms, one per name.
 +
# The format of the form is shown in the body of
 +
# the welcome() function.
 +
#
 +
 +
def welcome():
 +
    # displays welcome screen to user
 +
    print( """
 +
        +--------------------------------------+
 +
        |formgen.py        | Date:            |
 +
        |                  +-------------------+
 +
        |                                      |
 +
        +--------------------------------------+
 +
        |                                      |
 +
        |      AUTOMATIC FORM GENERATOR        |
 +
        |                                      |
 +
        |                                      |
 +
        |                                      |
 +
        +--------------------------------------+\n\n""" )
 +
 +
   
 +
def main():
 +
    welcome()
 +
   
 +
    # get data from the user
 +
    totalWidth = eval( input( "Width of box? " ) )
 +
    heightBox  = eval( input( "Height of blank box? " ) )
 +
    noNames = eval( input( "How many names? " ) )
 +
    names = []
 +
    for i in range( noNames ):
 +
        names.append( input( "> " ) )
 +
 +
 +
    # compute various quantities
 +
    bar        = "+" + '-' * (totalWidth-2 ) + '+'
 +
    blank      = '|' + ' ' * (totalWidth-2 ) + '|'
 +
    dateLine  = '| Date:            |'
 +
    dateLine2  = '+-------------------+'
 +
    noSpacesB4 = totalWidth-len( dateLine )-1
 +
    dateLine2  = '|'+' '*noSpacesB4 + dateLine2
 +
 +
    # space things out
 +
    print( "\n\n\n\n\n" )
 +
 +
    # output the forms for each name
 +
    for name in names:
 +
        print( bar )
 +
        totalNoSpaces  = totalWidth - 2 - len( name )
 +
        noSpacesB4Date = totalNoSpaces +1 - len( dateLine )   
 +
       
 +
        print( '|' + name + ' '* noSpacesB4Date + dateLine )
 +
        print( dateLine2 )
 +
        print( blank )
 +
        print( bar )
 +
        # print blank box
 +
        for i in range( heightBox ):
 +
            print( blank )
 +
        print( bar )
 +
        print()
 +
       
 +
    print()
 +
 +
 +
main()
 +
 +
</source>
 +
 +
 +
<br />
 +
 +
<br />
 +
 +
<br />
 +
 +
<br />
  
 +
<br />
  
>>>
+
<br />
 +
[[Category:CSC111]][[Category:Exercises]][[Category:Python]]

Latest revision as of 11:43, 27 September 2011

--D. Thiebaut 08:54, 27 September 2011 (EDT)


Exercise

Write a Python program that displays forms around names. The forms can be used to record information about candidates for a particular position, for example.

Below is an example of an interaction between the user and the finished program:

>>> ================================ RESTART ================================
>>> 

       +--------------------------------------+
       |formgen.py        | Date:             |
       |                  +-------------------+
       |                                      |
       +--------------------------------------+
       |                                      |
       |      AUTOMATIC FORM GENERATOR        |
       |                                      |
       |                                      |
       |                                      |
       +--------------------------------------+


Width of box?  40
Height of blank box? 4
How many names? 3
> Alice 
> Tintin et Milou
> Bugs Bunny






+--------------------------------------+
|Alice             | Date:             |
|                  +-------------------+
|                                      | 
+--------------------------------------+
|                                      |
|                                      |
|                                      |
|                                      | 
+--------------------------------------+

+--------------------------------------+
|Tintin et Milou   | Date:             |
|                  +-------------------+
|                                      |
+--------------------------------------+
|                                      |
|                                      |
|                                      |
|                                      | 
+--------------------------------------+

+--------------------------------------+
|Bugs Bunny        | Date:             |
|                  +-------------------+
|                                      | 
+--------------------------------------+
|                                      |
|                                      |
|                                      |
|                                      | 
+--------------------------------------+



>>>

Solution Program

# formgen.py
# D. Thiebaut
# generates a form with names of candidates
#
# the program first prompts the user
# for the width of the form, and the number
# of names for which forms will be generated
# Then the user enters the names and the program
# outputs the forms, one per name.
# The format of the form is shown in the body of
# the welcome() function.
#

def welcome():
    # displays welcome screen to user
    print( """
        +--------------------------------------+
        |formgen.py        | Date:             |
        |                  +-------------------+
        |                                      |
        +--------------------------------------+
        |                                      |
        |      AUTOMATIC FORM GENERATOR        |
        |                                      |
        |                                      |
        |                                      |
        +--------------------------------------+\n\n""" )

    
def main():
    welcome()
    
    # get data from the user
    totalWidth = eval( input( "Width of box? " ) )
    heightBox  = eval( input( "Height of blank box? " ) )
    noNames = eval( input( "How many names? " ) )
    names = []
    for i in range( noNames ):
        names.append( input( "> " ) )


    # compute various quantities
    bar        = "+" + '-' * (totalWidth-2 ) + '+'
    blank      = '|' + ' ' * (totalWidth-2 ) + '|'
    dateLine   = '| Date:             |'
    dateLine2  = '+-------------------+' 
    noSpacesB4 = totalWidth-len( dateLine )-1
    dateLine2  = '|'+' '*noSpacesB4 + dateLine2

    # space things out
    print( "\n\n\n\n\n" )

    # output the forms for each name
    for name in names:
        print( bar )
        totalNoSpaces  = totalWidth - 2 - len( name )
        noSpacesB4Date = totalNoSpaces +1 - len( dateLine )    
        
        print( '|' + name + ' '* noSpacesB4Date + dateLine )
        print( dateLine2 )
        print( blank )
        print( bar )
        # print blank box 
        for i in range( heightBox ):
            print( blank )
        print( bar )
        print()
        
    print()


main()