IN PYTHON CSE 231 Summer 2015 Programming Project 05 This assignment is worth 80
ID: 663654 • Letter: I
Question
IN PYTHON
CSE 231 Summer 2015 Programming Project 05 This assignment is worth 80 points (8.0% of the course grade) and must be completed and turned in before 11:59 on Monday, June 22.
Assignment Overview The goal of this project is to gain more practice with functions, files, exception handling, and dictionaries. Let’s tackle a political question. It is standard political dogma that Democrats “want big government” and that Republicans “create jobs.” Let’s see if data supports the dogma. The official source of employment statistics is the U.S. Bureau of Labor Statistics (http://www.bls.gov/data/#employment) and we have collected data for private employment and government employment. Our assumption is that if a party is creating jobs, then private employment will increase, and if a party is creating bigger government, then government employment will increase. Your job is to extract that information from the files provided.
Project Specification There are two employment data files provided. Both have comma-separated data. Look at the headings to understand their format. Numerical values are in thousands. The data for 2015 is, of course, incomplete. • government_employment.txt • private_employment.txt There is also a comma-separated file on presidents, their years, and their political party. • presidents.txt Since the transition from one term to another occurs partway through January, the last year listed for any president is the same as the first year of the next president. To keep things simple let’s count January entirely for the incoming president. That is, in the file the last year listed for a president will not count. For example, George W. Bush’s last year is listed as 2009 but he was president for only a few weeks that year so we will not count him as being president in 2009. Also, watch out for the “Jr.” for President Carter when you are reading the file—there is an extra comma. Finally, we will consider only the full years for each president so for Obama we will not count any data from 2015 (especially since the data for March and April is preliminary, not final data).
1. Your program will prompt for the file names. Use exceptions to check that each file was opened without an error.
2. All president data can only come from the presidents.txt file, i.e. you cannot code specifics about presidents into your program. We will test your program using a file of that format, but with different entries, for example we may test on a file that has three of the lines of that file.
3. As an academic requirement your program must define and use two functions in a meaningful way. (Feel free to use more functions; I did.)
4. Calculate and display in columns (see sample below): 1. the average monthly private employment for each political party 2. the average monthly government employment for each political party 3. the private employment of the first month and last month of each president 4. the change in private employment from the first month to the last month of each president; report the change as both a difference and as a percentage 5. the government employment of the first month and last month of each president 6. the change in government employment from the first month to the last month of each president; report the change as both a difference and as a percentage
5. Write a paragraph on your conclusions of the validity of the political dogma that “Democrats want big government and Republicans create jobs.” Refer to your program output as evidence for your conclusion. Put the paragraph in a separate document (either as proj05.txt, proj05.tex or proj05.doc) and hand it in. Feel free to be creative.
Hints 1. A dictionary is a good candidate for storing the employment data. 2. There is a useful formatting type for printing percentage print("{:6.2%}".format(1/3)) 33.33% 3. There is a useful formatting type that puts commas in numbers (either int or float): print(“{:,}”.format(123456}) 123,456
Sample Output (note that we will test on the provided president’s file as well as a subset of the file) Government employment average per month (millions) Republican: 19,019 Democratic: 19,548 Private employment average per month (millions) Republican: 94,750 Democratic: 97,313 Private Employment by president (millions) President First Month Last Month Difference Percentage Carter 65,636 74,820 9,184 14.0% Reagan 74,677 90,774 16,097 21.6% Bush 89,394 93,202 3,808 4.3% Clinton 90,904 109,633 18,729 20.6% Bush 111,861 107,203 -4,658 -4.2% Obama 111,398 118,690 7,292 6.5% Government Employment by president (millions) President First Month Last Month Difference Percentage Carter 15,056 16,073 1,017 6.8% Reagan 16,360 18,075 1,715 10.5% Bush 17,774 19,109 1,335 7.5% Clinton 18,901 21,355 2,454 13.0% Bush 20,835 22,482 1,647 7.9% Obama 22,579 21,902 -677 -3.0%
Explanation / Answer
#! /usr/bin/python
print " Employment statistics - government versus private president wise ";
# this python code will read from the data file and populate the dictionary in the following format
# GovtDict = { 'Carter' : 65636, 'Reagan' : 74677, 'Bush' : 89384, 'Clinton' : 90904, 'Obama' : 111398 };
# same format for the GovtDict as well but with the different data
# command to read in the file government employment
govtEmplmntFileName = raw_input("Please Enter file name for government_employment: ");
ipFileGovtEmp = open(govtEmplmntFileName,"r")
stringGovtEmplmnt = ipFileGovtEmp.read(80);
# read each line - one line per year
# private employment file handling
privtEmplmntFileName = raw_input("Please Enter file name for private_employment: ");
ipFilePrivtEmp = open(privtEmplmntFileName,"r")
stringPrivtEmplmnt = ipFilePrivtEmp.read(80);
# Presidents file handling
PrsdntDict = { }
PrsdntsFileName = raw_input("Please Enter file name for Presidents: ");
ipFilePrsdnts = open(prsdntsFileName,"r") . read() . split( )
stringPrsdnts = ipFilePrsdnts.read(80);
for i in range ( len (ipFilePrsdnts) ) :
if (ipFilePrsdnts [i] != "") :
LHS = ipFilePrsdnts [i].split (",")
RHS = ipFilePrsdnts [i]. split(",")
PrsdntDict [LHS] = RHS
theKeys = PrsdntsDict.keys()
theKeys.sort()
# get the current file position by using the tell command
postn = ipFilePrsdnts.tell();
# now populate the dictionary in the above mentioned format
GovtDict = {stringPrsdnts : stringGovtEmplmnt};
# out put part of the code
opFileName = raw_input("Please Enter file name for out put file: ");
opFile =
# open the output file for both appending and if the file does not exist create one
with open(opFileName, "ab+") as opFile :
for line in opFile :
opFile.write("{:6.2%}".format(1/3)}
# calculate average monthly private employment for each party
pvtTotal = pjan + pfeb + pmar + papr + pmay + pjun + pjuly + paug + psep + poct + pnov + pdec;
pvtAvg = pvtTotal / 12;
# calculate average monthly government employment for each party
govTotal = gjan + gfeb + gmar + gapr + gmay + gjun + gjuly + gaug + gsep + goct + gnov + gdec;
govAvg = govTotal / 12;
# private employment for the first month and last month for each president
pvtFirst = pjan
pvtLast = pdec
# The change in private employment from the first month to last month for each president
if pjan > pdec : pvtChange = pjan - pdec
if pdec > pjan : pvtChange = pdec - pjan
# government employment for the first month and last month for each president
govFirst = govjan
govLast = govdec
# The change in government employment from the first month to last month for each president
if govjan > govdec : govChange = govjan - govdec
if govdec > govjan : govChange = govdec - govjan
# open the output file for both appending and if the file does not exist create one
with open(opFileName, "ab+") as opFile :
for line in opFile :
opFile.write("{:6.2%}".format(1/3)} pvtChange govChange )
opFile.write("{:6.2%}".format(1/3)} pjan pfeb pmar papr pmay pjun pjuly paug psep poct pnov pdec);
opFile.write("{:6.2%}".format(1/3)} gjan gfeb gmar gapr gmay gjun gjuly gaug gsep goct gnov gdec);