Question
Manipulate lists of state populations Author: File: ch4_states.py Course: print('This program works with more states and their populations') # These are the lists
""" Manipulate lists of state populations Author: File: ch4_states.py Course: """
print('This program works with more states and their populations')
# These are the lists that you'll be working with 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
# Step 1: Edit the data file path # The next line DATA_FILE = you'll need to edit. Python uses this string to find the data file. # Use a file manager to see the full path of this file, then edit the text string 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 text) # # I happen to be using a directory called CIS156 under Documents. # What you see below is on a Windows system... on a Mac this might look like # r'/Users/david/Documents/CIS156/us_state_info.csv'
DATA_FILE = r'O:\CIS156\Programs\Chapter_4\us_state_info.csv'
print("Load data from this file:", DATA_FILE) # quick check to see if the filename looks ok
# We're starting to move toward programs with a bit more structure, but don't let that throw you. # This week you'll be putting your code inside a function called main, which begins right below this comment. 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 print_all_states()
print(' Part 1 - check the first five entries for Arizona') # Check if any of the first five entries in state_names is Arizona. If yes, print a line that # indicates you found Arizona somewhere in the first five entries. Otherwise, print a line # that says you didn't find Arizona in the first five entries. # Hint: several ways to tackle this... you could use a series of if.. elif statements. # You could use a series of if statements that keep track on any matches you find, perhaps using a boolean variable. # You could get a slice of the first five in state_names and use a membership test. # insert your code next - IMPORTANT... indent your code to line up with the beginning of this comment # this indentation should happen automatically in IDLE... # just put your cursor at the end of this line and press Enter
print(' Part 2 - check the first three entries for big population Gulf states') # Check if any of the first three entries in the lists meet the following criteria: # populations equal to or greater than 25 million, and # have a coastline on the Gulf of Mexico (Florida, Alabama, Mississippi, Louisiana, or Texas) # If yes,print out the name(s) of those states... you can print multiple lines as desired. # You only need to check the first three entries... otherwise, you'd be typing a lot # of statements. # Hints: Reminder of the boolean operators (zyBooks Participation 4.5.2) to test multiple conditions, # and consider putting the coastal state names in a list, then doing a membership comparision # (zyBooks Figure 4.6.1).
# insert your code next - IMPORTANT... indent your code to line up with this comment
# what follows are a couple of functions that help this program.
def get_all_states(): """Opens a .csv file and loads lists with state names and July 2019 population data"""
infile = open(DATA_FILE, 'r')
for line in infile: one_line = line.split(',') state_names.append(one_line[0]) x = int(one_line[1]) state_populations.append(x) 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) print(state_populations)
# call main main()
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