Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello this is for a lab in Python 3 class, and what the purpose is we are using loops to manipulate the lists in the

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Hello this is for a lab in Python 3 class, and what the purpose is we are using loops to manipulate the lists in the US populations. Any help would be appreciated thank you. Btw attached are the photos from the lab.

5_states (1).py - C:\Users\sabel\Downloads\ch5_states (1).py (3.9.2) Edit Format Run Options Window Help Using loops to manipulate lists of state populations Author: Maxwell Sabel File: ch5 states.py Course: CIS 156 print(" Chapter 5: This program uses loops to analyze states and their populations') # These are the lists that you'll be working with # This week's list of states is not in the same order as those used for Chapter 4. # (side note: might be interesting to see if your chapter 4 program works with this new datase state names = [] # a list of all the state names as strings, in no particular order state populations = [ # a list of those state's populations as integers # FIRST STEP: Edit the data input file path # The next line INPUT FILE = you'll need to edit. Python uses this string to find the data fil # Use a file manager to see the full path of this file, then edit the text str inside the single quotes to match your system. Leave the leading lowercase r in place (this tells Python not to react to the symbols in the # I happen to be using a directory called CIS156\Ch 5 under Documents. The string in my code is on a Windows system... on a Mac this might look like r' /Users/david/Documents/CIS156/ch 5/ch5 us state info.csv" INPUT_FILE = r'C:\Users\sabel\OneDrive Desktop\CIS 156 Python files\ch5_us_state_info.csv' print("Data file:", INPUT_FILE) # quick check to see if the filename looks ok one # This week you'll again be putting your code inside functions... this time, 4 for each part of the assignment det parti(): print(" Part - 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 # 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 2, which should match" #insert your code next... just put your cursor at the end of this line and press Enter max_population = 0 Orlooplin range (len ('population')): if (population' ['state_populations'] [loopl] > max_population) : max_population = population['state populations'][loopl] index = loopl #storing the index where the max population is print("The biggest value is", population['Californa'][index], 'with a population of',39512223) Using max() to find the max population max_population2 = max (population ('state_populations'] Type here to search DOLL h5_states (1).py - C:\Users\sabel\Downloads\ch5_states (1).py (3.9.2) Edit Format Run Options Window Help det part20: 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 isx" # followed with something like "Using sum() I get y, which should match" # insert your code next... def part30: print(" Part 3 Where does Arizona rank?'); # Use a loop to scan the list and determine where Arizona ranks in the pecking order by p # 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 # 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 w # 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 th # Print the list. # If the list is empty, print something like. "There were no states with that letter" and a: # Hint: an easy way to do the input is making a string of acceptable letters, something 1 # x = 'abcdef... ZABC...2 # then test if user input is a member of that string. # insert your code next... def part5(): Type here to search O ch5_states (1).py - C:\Users\sabel\Downloads\ch5_states (1).py (3.9.2) File Edit Format Run Options Window Help 119 120 121 122 # what follows are a few functions that help this program. 123 # it's not dangerous to look down here... see if you can figure out what's going on 124 125 126 # what follows are a couple of functions that help this program. 127 def get all states(): 128 "Opensa .csv file and loads lists of state names and July 2019 population data 129 The .csv file is assumed to have a header row, but we skip over it." 130 131 infile = open(INPUT_FILE, 'r') 132 133 next (infile) # read and toss away the header which is the first line in the file 134 135 for line in infile: 136 one_line = line.split(',') # obtain one line from the file, parse it using the 137 state_names.append (one_line [0]) # append this name to the tail of the list 138 x = int (one line[1]). # convert the population string into integer 139 state populations.append(x) # and then append to the tail of the list, repea 140 infile.close() 141 142 def print_all_states (): 143 Quick check that the data loaded properly" 144 print(" our lists have', len (state names), 'state names and', len (state populations 145 print("state names:", state_names) 146 print() 147 print("state populations:", state populations) 148 149 def goodbye(): 150 print(" ***End of program***") 151 152 def main (): 153 get_all states () #this loads the two lists 154 #print all states () # quick check to see that everything was loaded... uncomment this 155 parti() # run each of the assignment parts 156 part2) 157 part3) 158 part4) 159 part5) 160 goodbye 161 162 # call main 163 # ok, granted, the next two lines of code are odd... just consider it to be magic code at 164 # don't worry, all will be revealed in good time! Start a disussion thread or send me an 165 if main 166 main() 167 168 name Type here to search BI View Calibri X Cut [Copy Format Painter 11 CA A a Wrap Te Paste B I V - - A - Es Merge & Clipboard 5 Font 21 Alignment AN f State Name D E F G H A B 1 State Nam State Population 2 Ohio 11689100 3 Arkansas 3017825 4 Idaho 1792065 5 Connecticu 3565287 6 North Caro 10488084 7 Virginia 8535519 8 West Virgil 1787147 9 Ilowa 3155070 10 Wyoming 578759 11 Wisconsin 5822434 12 Pennsylvar 12801989 13 New Hamp 1359711 14 Missouri 6137428 15 Oklahoma 3956971 16 Vermont 623989 17 Delaware 973764 18 California 39512223 19 Kansas 2913314 20 Alaska 731545 21 Michigan 9986857 22 New York 19453561 23 Texas 28995881 24 Illinois 12671821 25 South Dako 884659 26 Nebraska 1934408 27 Utah 3205958 28 Minnesota 5639632 29 Louisiana 4648794 30 Arizona 7278717 31 Colorado 5758736 32 Tennessee 6833174 33 Maine 1344212 34 Alabama 4903185 35 New Jerses 8882190 36 Mississippi 2976149 37 Rhode Islai 1059361 38 Hawaii 1415872 39 North Dako 762062 40 Georgia 10617423 ch5_us state info

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

Students also viewed these Databases questions

Question

Describe the Indian constitution and political system.

Answered: 1 week ago

Question

Explain in detail the developing and developed economy of India

Answered: 1 week ago

Question

Problem: Evaluate the integral: I = X 52+7 - 1)(x+2) dx

Answered: 1 week ago

Question

What is gravity?

Answered: 1 week ago

Question

What is the Big Bang Theory?

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago