Question
Instructions Below- Complete the functions get_list_from_file() and get_listing_in_range() When you first run the program, you will see: IndexError: list index out of range -- consider
Instructions Below- Complete the functions get_list_from_file() and get_listing_in_range()
When you first run the program, you will see: "IndexError: list index out of range" -- consider why that would be. Print the states variable so you can see it. What should when the get_list_from_file() function is complete? Answer: you should get a list lists: the sublists contain the dats: state names and their populations. Before even writing the function, try returning some dummied up properly data that is properly formated. Ex: [["West Dakota", 111], ["Old York", 9999999]]
Test your get_list_from_file() after it is returning proper data properly formated. Use print statements to check.
get_listing_in_range() takes three arguments. Check the way the function in called from main so that you understand the datatypes of the arguments. Think about what your code should do before writing any. Requirements Below -
get_list_from_file
open census_data file for reading, and assign the file to a variable
loop through each line in the file...
split name and population into a list (ie: a state),
and add that state to the list of states
get_listing_in_range
the count variable is correct integer
the listing variable contains the correct data
order is correct: states are printed from lowest population to highest
string formatting is correct: (-- see transcript, below)
-tab character,
-state left justified on a field of 20 characters,
-population of state,
-newline character
float rounding is correct (population given as a number of millions rounded to 2 decimal places -- see transcript),
the function is pure Starting code below:
def get_list_from_file(census_data): # open census_data file for reading, and assign the file to a variable states = [] # loop through each line in the file... # split name and population into a list (ie: a state),co # and add that state to the list of states return states def get_listing_in_range(lower, upper, state_list): listing = "" count = 0 # your code here... print(count, "States have a population between", lower, "and", upper, "million:") return listing def main(): # TODO: complete the get_list_from_file() function states = get_list_from_file("census2020.txt") print(" ***first state in list:", states[0][0], '*** ') # should be California print("The least populous U.S. state: Wyoming with just over 0.5 million") print("The most populous U.S. state: California with almost 40 million") print("Enter two numbers between 0.5 and 40 to list states in that range.") lo = float(input("Enter lower bound: ")) hi = float(input("Enter upper bound: ")) # TODO: complete the function called get_listing_in_range(); make it a pure function # The return value should be a string. # Each state and population in that range should appear on its own line, # ordered by population from lowest to highest. listing = get_listing_in_range(lo, hi, states) print(listing) print(" ***first state in list:", states[0][0], '*** ') # should be California main() Census for states below-
California 39538223 Texas 29145505 Florida 21538187 New_York 20201249 Pennsylvania 13002700 Illinois 12812508 Ohio 11799448 Georgia 10711908 North_Carolina 10439388 Michigan 10077331 New_Jersey 9288994 Virginia 8631393 Washington 7705281 Arizona 7151502 Massachusetts 7029917 Tennessee 6910840 Indiana 6785528 Maryland 6177224 Missouri 6154913 Wisconsin 5893718 Colorado 5773714 Minnesota 5706494 South_Carolina 5118425 Alabama 5024279 Louisiana 4657757 Kentucky 4505836 Oregon 4237256 Oklahoma 3959353 Connecticut 3605944 Utah 3271616 Iowa 3190369 Nevada 3104614 Arkansas 3011524 Mississippi 2961279 Kansas 2937880 New_Mexico 2117522 Nebraska 1961504 Idaho 1839106 West_Virginia 1793716 Hawaii 1455271 New_Hampshire 1377529 Maine 1362359 Rhode_Island 1097379 Montana 1084225 Delaware 989948 South_Dakota 886667 North_Dakota 779094 Alaska 733391 District_of_Columbia 689545 Vermont 643077 Wyoming 576851 Sample Transcript Below-
***first state in list: California ***
The least populous U.S. state: Wyoming with just over 0.5 million The most populous U.S. state: California with almost 40 million Enter two numbers between 0.5 and 40 to list states in that range. Enter lower bound: 1 Enter upper bound: 2 8 States have a population between 1.0 and 2.0 million:
Nebraska 1961504 Idaho 1839106 West_Virginia 1793716 Hawaii 1455271 New_Hampshire 1377529 Maine 1362359 Rhode_Island 1097379 Montana 1084225
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