Difference between revisions of "CSC111 A Form Generator Program"
(→Exercise) |
(→Solution Program) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 70: | Line 70: | ||
+--------------------------------------+ | +--------------------------------------+ | ||
+ | |||
+ | |||
+ | >>> | ||
+ | ==Solution Program== | ||
− | |||
− | |||
− | |||
− | |||
− | |||
<source lang="python"> | <source lang="python"> | ||
# formgen.py | # formgen.py | ||
Line 140: | Line 139: | ||
print( blank ) | print( blank ) | ||
print( bar ) | print( bar ) | ||
+ | # print blank box | ||
for i in range( heightBox ): | for i in range( heightBox ): | ||
print( blank ) | print( blank ) | ||
Line 151: | Line 151: | ||
</source> | </source> | ||
− | + | ||
<br /> | <br /> |
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()