Question
def open_file(ch) -> file_pointer i. Open a file for reading. Repeatedly prompt for a file name until a file is successfully opened. Use the try-except
def open_file(ch) -> file_pointer
i. Open a file for reading. Repeatedly prompt for a file name until a file is successfully opened. Use the try-except command: except FileNotFoundError The prompt: 'Input a file: '
The error message: 'Invalid filename, please try again.'
-
Parameters: none
-
Returns: file_pointer
-
Display: prompt and error message as appropriate
b. get_county_state(s) -> str, str
-
The parameter is s, a string, with the county and state separated by a comma.
Split the line and return the county and state, comma-separated. Remember to strip leading and trailing spaces. For example if s = "Autauga County, AL" Return "Autauga County", "AL"
-
Parameters: str
-
Returns: str, str
-
Display: nothing
c. def read_file(fp) -> list of tuples
-
The parameter is a file pointer. Read the whole file to extract data from columns
1 and 10; return a list of tuples. Because column 1 has commas within the field and the file is a comma-separated-value file (csv) use the csv module (see program notes below). There is one header line that needs to be skipped. Column 1 has the county and state, separated by a comma. The state is the two- letter abbreviation. Call the get_county_state to separate the county and state. Column 10 has the median income for the county. The income is an integer, but values over a thousand have commas that need to be handled. If there is no value for income, ignore that county. All other columns can be ignored. Return a list of tuples where each tuple is
(state, county, median_income) Sort the list in decreasing order of median_income. Hint: use itemgetter from the operator module (see program notes below).
-
Parameters: file pointer
-
Returns: sorted list of tuples
-
Display: nothing
d. def state_average_income(state, master_list) -> float
i. Calculate and return the average of the median incomes for the counties in the specified state rounded to 2 decimals. master_list is the list created by read_file(), it is a list of tuples where each tuple is
(state, county, median_income) The function should return None if the list of tuples is empty, or if the state does not exist in the master_list.
ii. Parameters: str, list of tuples iii. Returns: float or None iv. Display: nothing
e. def top_counties_by_income(master_list) -> list of tuples
-
Select and return the top ten counties by median incomes in decreasing order.
The tuples are the same as in the master_list.
-
Parameters: list of tuples
-
Returns: sorted list of ten tuples
-
Display: nothing
f. def bottom_counties_by_income(master_list) -> list of tuples
-
Select and return the bottom ten counties by median incomes in decreasing
order. The tuples are the same as in the master_list.
-
Parameters: list of tuples
-
Returns: sorted list of ten tuples
-
Display: nothing
g. def top_states_by_income(master_list) -> list of tuples
-
Select and return the top ten states by average median incomes in decreasing
order. The function should return a list of tuples where each tuple is
(state, average_median_income) Hint: call state_average_income for each state, sort, and return; use itemgetter from the operator module (see program notes below)
-
Parameters: list of tuples
-
Returns: sorted list of ten tuples
-
Display: nothing
h. def bottom_states_by_income(master_list) -> list of tuples
-
Select and return the bottom ten counties by median incomes in decreasing
order. The function should return a list of tuples where each tuple is
(state, average_median_income) Hint: call state_average_income for each state, sort, and return; use itemgetter from the operator module (see program notes below). Make sure to not include states that dont have data in master_list (those states should return None when called with state_average_income)
-
Parameters: list of tuples
-
Returns: sorted list of ten tuples
-
Display: nothing
i. def counties_in_state(state,master_list) -> list of tuples
-
Select and return a list of tuples with the counties and their median incomes in
the state sorted in ascending order (alphabetically). master_list is a list of tuples where each tuple is
(state, county, median_income)
The function should return a list of tuples where each tuple is
(county, median_income)
-
Parameters: list of tuples
-
Returns: sorted list of tuples
-
Display: nothing
j. def main()Your main algorithm will be as follows calling the above functions. 1. Call open_file to open an input file for reading 2. Call read_file to read the desired data into a master list of tuples. 3. Display a menu of options and prompt for input (use the display_options
function to display the menu). 4. Loop until the input is " q "
Based on the input:
-
a) If option is '1', Call state_average_income to determine the average
median income for a state; display it. Your code should prompt the user to input a 2-letter code. If it is valid (if it is one of the codes included in the STATES list provided in the starter code), the average median income for a state should be displayed formatted as
' Average median income in {:2s}: ${:
-
b) If option is '2', Call top_counties_by_income to determine the top 10
counties and their income; display them. You need first to display two header
lines:
' Top 10 Counties by Median Household Income (2018)' '{:
The format for all the numbers is as follow:
'{:
c) If option is '3', Call bottom_counties_by_income a function to determine the bottom 10 counties and their income; display them. You need first to display two header lines:
' Bottom 10 Counties by Median Household Income (2018)' '{:
The format for all the numbers is as follow:
'{:
d) If option is '4', Call top_states_by_income to determine the top 10
states by income; display them. You need first to display two header lines:
' Top 10 States by Average Median Household Income (2018)' '{:
The format for all the numbers is as follow:
'{:
-
e) If option is '5', Call bottom_states_by_income to determine the
bottom 10 by income; display them. You need first to display two header lines:
' Bottom 10 States by Average Median Household Income (2018)' '{:
The format for all the numbers is as follow:
'{:
-
f) If option is '6', Display all the counties and income for a state. Your code should prompt the user to input a 2-letter code. If it is valid (if it is one of the codes included in the STATES list provided in the starter code), you should first display the number of counties in a state in alphabetical order. If there are no counties in a state, use the following formatting:
' There are 0 counties in {}'
If there are at least 1 county in the state, use the following formatting:
' There are {} counties in {}:'
then print the average median income for each county in the state (if they exist). You need to print a header line:
'{:
All the numbers should be formatted as:
'{:
-
g) Display an error message if it is an invalid option.
5. Display the options again and prompt for input. data_small_csv:
FIPStxt,area_name,Civilian_labor_force_2018,Employed_2018,Unemployed_2018,Unemployment_rate_2018, Civilian_labor_force_2019 , Employed_2019 , Unemployed_2019 ,Unemployment_rate_2019,Median_Household_Income_2018,Med_HH_Income_Percent_of_State_Total_2018 01001,"Autauga County, AL","26,196","25,261",935,3.6,"26,172","25,458",714,2.7,"59,338",119.0 01003,"Baldwin County, AL","95,233","91,809","3,424",3.6,"97,328","94,675","2,653",2.7,"57,588",115.5 06001,"Alameda County, CA","842,427","816,611","25,816",3.1,"844,346","819,675","24,671",2.9,"101,744",135.2 06003,"Alpine County, CA",547,522,25,4.6,551,523,28,5.1,"55,384",73.6 06005,"Amador County, CA","14,686","14,084",602,4.1,"14,900","14,328",572,3.8,"58,515",77.8 06007,"Butte County, CA","101,932","96,749","5,183",5.1,"99,242","94,162","5,080",5.1,"50,945",67.7 06009,"Calaveras County, CA","21,188","20,329",859,4.1,"21,428","20,616",812,3.8,"62,984",83.7 72147,"Vieques Municipio, PR","2,718","2,269",449,16.5,"2,585","2,406",179,6.9,, 72149,"Villalba Municipio, PR","7,311","6,154","1,157",15.8,"7,406","6,231","1,175",15.9,, 72151,"Yabucoa Municipio, PR","8,762","7,509","1,253",14.3,"8,691","7,552","1,139",13.1,, 72153,"Yauco Municipio, PR","9,716","8,288","1,428",14.7,"9,759","8,331","1,428",14.6,,
strings.txt:
'Input a file: ' 'Invalid filename, please try again.' " Median Income Data" 'Choose an option, q to quit: ' 'Please enter a 2-letter state code: ' 'Please input a valid state' ' Average median income in {:2s}: ${:template for project:
import csv from operator import itemgetter STATES = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"] def open_file(): """Insert Docstring here""" pass def get_county_state(s): """Insert Docstring here""" pass def read_file(fp): """Insert Docstring here""" pass def state_average_income(state, master_list): """Insert Docstring here""" pass def top_counties_by_income(master_list): """Insert Docstring here""" pass def bottom_counties_by_income(master_list): """Insert Docstring here""" pass def top_states_by_income(master_list): """Insert Docstring here""" pass def bottom_states_by_income(master_list): """Insert Docstring here""" pass def counties_in_state(state, master_list): """Insert Docstring here""" pass def display_options(): """ DO NOT CHANGE Display menu of options for program """ OPTIONS = """ Menu 1: Average median household income in a state 2: Highest median household income counties 3: Lowest median household income counties 4: Highest average median household income states 5: Lowest average median household income states 6: List counties' median household income in a state """ print(OPTIONS) def main(): print(" Median Income Data") pass if __name__ == '__main__': main()a. def open_file (ch) file_pointer i. Open a file for reading. Repeatedly prompt for a file name until a file is successfully opened. Use the try-except command: except FileNotFoundError The prompt: 'Input a file: The error message: 'Invalid filename, please try again. ii. Parameters: none iii. Returns: file_pointer iv. Display: prompt and error message as appropriate b. get_county_state (s) str, str i. The parameter is s, a string, with the county and state separated by a comma. Split the line and return the county and state, comma separated. Remember to strip leading and trailing spaces. For example if s = "Autauga County, AL" Return "Autauga County", "AL" ii. Parameters: str iii. Returns: str, str iv. Display: nothing c. def read_file(fp) list of tuples i. The parameter is a file pointer. Read the whole file to extract data from columns 1 and 10; return a list of tuples. Because column 1 has commas within the field and the file is a comma-separated-value file (csv) use the csv module (see program notes below). There is one header line that needs to be skipped. Column 1 has the county and state, separated by a comma. The state is the two- letter abbreviation. Call the get county state to separate the county and d. def state_average_income (state, master_list) float i. Calculate and return the average of the median incomes for the counties in the specified state rounded to 2 decimals. master_list is the list created by read_file(), it is a list of tuples where each tuple is (state, county, median_income) The function should return None if the list of tuples is empty, or if the state does not exist in the master list. ii. Parameters: str, list of tuples iii. Returns: float or None iv. Display: nothing e. def top_counties_by_income (master_list) list of tuples i. Select and return the top ten counties by median incomes in decreasing order. The tuples are the same as in the master_list. ii. Parameters: list of tuples iii. Returns: sorted list of ten tuples iv. Display: nothing f. def bottom_counties_by_income (master_list) list of tuples i. Select and return the bottom ten counties by median incomes in decreasing order. The tuples are the same as in the master_list. ii. Parameters: list of tuples iii. Returns: sorted list of ten tuples iv. Display: nothing g. def top_states_by_income (master_list) list of tuples g. def top_states_by_income (master_list) list of tuples i. Select and return the top ten states by average median incomes in decreasing order. The function should return a list of tuples where each tuple is (state, average_median income) Hint: call state_average_income for each state, sort, and return; use itemgetter from the operator module (see program notes below) ii. Parameters: list of tuples iii. Returns: sorted list of ten tuples iv. Display: nothing h. def bottom_states_by_income (master_list) list of tuples i. Select and return the bottom ten counties by median incomes in decreasing order. The function should return a list of tuples where each tuple is (state, average_median income) Hint: call state_average_income for each state, sort, and return; use itemgetter from the operator module (see program notes below). Make sure to not include states that don't have data in master_list (those states should return None when called with state_average_income) ii. Parameters: list of tuples iii. Returns: sorted list of ten tuples iv. Display: nothing i. def counties_in_state (state, master_list) list of tuples i. Select and return a list of tuples with the counties and their median incomes in the state sorted in ascending order (alphabetically). master_list is a list of tuples where each tuple is (state, county, median_income) The function should return a list of tuples where each tuple is (county, median_income) ii. Parameters: list of tuples iii. Returns: sorted list of tuples iv. Display: nothing j. def main() Your main algorithm will be as follows calling the above functions. 1. Call open_file to open an input file for reading 2. Call read_file to read the desired data into a master list of tuples. 3. Display a menu of options and prompt for input (use the display_options function to display the menu). 4. Loop until the input is " q Based on the input: a) If option is 'l', Call state_average_income to determine the average median income for a state; display it. Your code should prompt the user to input a 2-letter code. If it is valid (if it is one of the codes included in the STATES list provided in the starter code), the average median income for a state should be displayed formatted as ' Average median income in {:25}: ${:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started