Question: GOAL: Your program should prompt the user for a state ( Vermont , New Hampshire, or Maine ) , read the corresponding CSV file, and

GOAL: Your program should prompt the user for a state (Vermont, New Hampshire, or Maine), read the corresponding CSV file, and calculate the total population for the state and the average per capita income for the state. Your program should then list the counties in the state with higher than average per capita income. Actually, once you have your program working correctly for the three CSV files provided, it should work correctly for any state data provided in similar format.
To calculate the average per capita income for the state you cannot merely average the values in the per capita income column. Why? Because different counties have different populations! The correct way to do this, given the data available, is to take the per capita income for a county, multiply it by the population for the county, and add this to an accumulator. Then, when all county data has been processed, and youve calculated the total population for the state, divide the value of the accumulator by the total population. This will give you average (mean) per capita income for the state.
Remember: When a CSV reader reads data from a file, each row in the file is retrieved as a list of strings. So dont forget to convert numeric strings to a suitable numeric type (for example, int) in order to perform calculations.
In the main body of your program, under if __name__=='__main__':
Open the file for reading using a context manager
Within the context for reading
Instantiate a CSV reader using the csv.reader() constructor.Use next() to skip the column headings.Read data and perform calculations as needed
Perform additional calculations as needed
Use the string method .title() to convert input string to proper case
Print the following
Population of statePer capita income for stateCounties with higher than average per capita income
Heres what a run of your program should look like.
Enter name of state: vermont The total population of Vermont is 625,741. Vermont per capita income is $27,489. The following counties have per capita income above the state average: Chittenden Grand Isle Windsor Washington Bennington
Notice that the order of counties listed with above average per capita income is the order in which they appear in the input CSV file.
How to test your program
Test your program on inputs vermont,new hampshire, and maine. Be sure to check that case variations do not result in a failure (FileNotFoundError or similar). That is, check things like vERMONt,maIne, or NEW hampshire.
You can see the expected output for Vermont, above. Heres what it should look like for Maine and New Hampshire:
Enter name of state: maine The total population of Maine is 1,328,361. Maine per capita income is $25,393. The following counties have per capita income above the state average: Cumberland Lincoln York Sagadahoc Hancock
Enter name of state: new hampshire The total population of New Hampshire is 1,377,529. New Hampshire per capita income is $41,257. The following counties have per capita income above the state average: Rockingham Carroll Hillsborough
The autograder will supply inputs vermont,maine, and new hampshire on separate runs of your program and will look for total population, with comma separators on the first line after the prompt. Then it will look for per capita income on the second line after the prompt. This should be formatted with currency symbol, comma separator, to 0 decimal places precision. Then the autograder will look for output listing above average counties as shown above.
The autograder will also test with a data file for another US state not provided as part of the assignment, to ensure that your program correctly reads and calculates results for any arbitrary state (and not just the three provided).
What to submit
You need only submit census.py. The autograder will supply the needed CSV files and will check the output of your program. You do not need to submit vermont.csv, maine.csv, or new_hampshire.csv.
Graders are authorized to deduct up to 10 points per program for submissions that do not follow the recipe provided above.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!