Question: Python-Lab: print('Chapter 5: This program uses loops to analyze states and their populations # This week you'll again be putting your code inside functions... this
Python-Lab:
print('Chapter 5: This program uses loops to analyze states and their populations
# This week you'll again be putting your code inside functions... this time, one # for each part of the assignment
def part1(): print(' Part 1 - use a loop to find the biggest state and population, compare to max()') # Using a loop, scan the entire list of populations to find the biggest. Then retrieve that state's name. # Print a line that indicates that state's name and its population. # Print also that result of the max() function on state_populations for comparison. # print out something like "The biggest state is x, with a population of y" # followed with something like "Using max() I get z, which should match" # insert your code next... just put your cursor at the end of this line and press Enter
def part2(): print(' Part 2 - use a loop to find all the states populations, compare to sum()') # Using a loop, scan the entire list to add up all the state populations. # Print also the value of the sum() function for comparison. # print something like "The total population of all the states is x" # followed with something like "Using sum() I get y, which should match" # insert your code next...
def part3(): print(' Part 3 - Where does Arizona rank?') # Use a loop to scan the list and determine where Arizona ranks in the pecking order by population. # print something like "Arizona ranks as the x th largest state in the nation" # Hint: review this week's Digging Deeper Additional Information for a review of rank. # insert your code next...
def part4(): print(' Part 4 - use a loop build a list and then print all the states containing a user supplied letter') # Input the user letter choice, using any non-letter entry to stop the loop # use something like user_input = input('Find all the states with letter (any non-letter will stop):') # Treat lower case and upper case as different letters. # Example: User requests lowercase p, result is Mississippi and New Hampshire. # Example: Uppercase P gives Pennsylvania. # Example: Lowercase g should return seven states, uppercase G returns only one. # If a valid letter is supplied, then gather into a list all the state names containing that letter. # Print the list. # If the list is empty, print something like "There were no states with that letter" and ask again. # Hint: an easy way to do the input is making a string of acceptable letters, something like # x = 'abcdef...zABC...Z' # then test if user_input is a member of that string. # insert your code next...
def part5(): print(' Part 5 - OPTIONAL Bonus Points - Biggest vs. the rest of the nation') # Use a loop to determine how many big states it takes to be half or more of the entire country's population # print something like # "The top x states, [list of those states], have a combined population of x, which is y % of the nation." # insert your code next...
# what follows are a few functions that help this program. # it's not dangerous to look down here... see if you can figure out what's going on
# what follows are a couple of functions that help this program. def get_all_states(): """Opens a .csv file and loads lists of state names and July 2019 population data The .csv file is assumed to have a header row, but we skip over it."""
infile = open(INPUT_FILE, 'r')
next(infile) # read and toss away the header which is the first line in the file
for line in infile: one_line = line.split(',') # obtain one line from the file, parse it using the commas state_names.append(one_line[0]) # append this name to the tail of the list x = int(one_line[1]) # convert the population string into integer state_populations.append(x) # and then append to the tail of the list, repeat the loop till EOF infile.close()
def print_all_states(): """Quick check that the data loaded properly""" print(' Our lists have', len(state_names), 'state names and', len(state_populations), 'populations.') print("state_names:", state_names) print() print("state_populations:", state_populations)
def goodbye(): print(" ***End of program***")
def main(): get_all_states() #this loads the two lists #print_all_states() # quick check to see that everything was loaded... uncomment this line to peek part1() # run each of the assignment parts part2() part3() part4() part5() goodbye()
# call main # ok, granted, the next two lines of code are odd... just consider it to be magic code at this point. # don't worry, all will be revealed in good time! Start a disussion thread or send me an email if you can't wait. if __name__ == '__main__': main()
Also here is the state population list that I am working with on this lab.
| State Name | State Population |
| Ohio | 11689100 |
| Arkansas | 3017825 |
| Idaho | 1792065 |
| Connecticut | 3565287 |
| North Carolina | 10488084 |
| Virginia | 8535519 |
| West Virginia | 1787147 |
| Iowa | 3155070 |
| Wyoming | 578759 |
| Wisconsin | 5822434 |
| Pennsylvania | 12801989 |
| New Hampshire | 1359711 |
| Missouri | 6137428 |
| Oklahoma | 3956971 |
| Vermont | 623989 |
| Delaware | 973764 |
| California | 39512223 |
| Kansas | 2913314 |
| Alaska | 731545 |
| Michigan | 9986857 |
| New York | 19453561 |
| Texas | 28995881 |
| Illinois | 12671821 |
| South Dakota | 884659 |
| Nebraska | 1934408 |
| Utah | 3205958 |
| Minnesota | 5639632 |
| Louisiana | 4648794 |
| Arizona | 7278717 |
| Colorado | 5758736 |
| Tennessee | 6833174 |
| Maine | 1344212 |
| Alabama | 4903185 |
| New Jersey | 8882190 |
| Mississippi | 2976149 |
| Rhode Island | 1059361 |
| Hawaii | 1415872 |
| North Dakota | 762062 |
| Georgia | 10617423 |
| Washington | 7614893 |
| New Mexico | 2096829 |
| Oregon | 4217737 |
| South Carolina | 5148714 |
| Montana | 1068778 |
| Indiana | 6732219 |
| Nevada | 3080156 |
| Maryland | 6045680 |
| Florida | 21477737 |
| Massachusetts | 6949503 |
| Kentucky | 4467673 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
