There is one deliverable for this assignment
This script must read in a text file of cities and the states in which they are located
The file has entries like the following
New York City,New York Los Angeles,California Chicago,Illinois ...
It should use this file to create a dictionary where the keys are state names and the values are a list of cities in that state.
The values of the second dictionary must be the rounded average of quiz scores for each student.
The script must have 3 functions:
This function must have the following header:
def open_file_read(filename):
It must try to create a file object for reading on the file whose name is given by the parameter filename.
If it is succesful in creating the file object it should return the object.
If it is cannot create the object it should print an error message and return None
.
This function must have the following header:
def cities_dictionary_create(file):
The function must create a dictionary where the state is the key and the value is a list of cites.
The list of cities should be updated as new cities are read from the file.
The function should use the following algorithm.
create the empty dictionary cities
for each line in the file:
get values for city and state from the line
if state not in cities
create a new dictionary entry using city and state
else
get the cities_list of state from cities
add city to cities_list
set the cities entry for state to cities_list
return cities
This function must have the following header:
def print_cities(dict):
The function must print the states and the cities they contain in alphabetical order.
The cities printed must be a string not a list.
In other words, must turn the list of cities into a string and print that string.
The function should use the following algorithm.
for in dict:
set cities_string to the empty string
set cities_list to the list of cities for state
for city in cities_list:
append city and a space to cities_string
print state and cities_string
This function must have the following header:
def print_city_count(dict):
The function must print the states and the number cities they contain in alphabetical order.
The function should use the following algorithm.
for state in dict:
print state and the length of the cities_list for state
Open an a text editor and create the file hw4.py.
You can use the editor built into IDLE or a program like Sublime.
Your hw4.py file must contain the following test code at the bottom of the file:
file = open_file_read("cities.csv") cities = cities_dictionary_create(file) if cities: print_cities(cities) print() print_city_count(cities)
For this test code to work, you must copy cities.csv to your machine.
To do this use FileZilla to copy the file cities.csv from /home/ghoffman/course_files/it117_files into the directory that holds your hw4.py script.
Write this program in a step-by-step fashion using the technique of incremental development.
In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.
pass
. pass
statement in open_file_read
with the body of the code
from your hw3.py script. pass
statement from
cities_dictionary_create. for
loop that loops through the file object file
using line as the loop variable. for
loop use the strip
method
to remove the linefeed character from . print
statement. split
method
on the string line. print
statement. if
statement that runs if state
is not already in the cities dictionary. in
operator on cities
to do this. if
statement, create the empty list
cities_list. if
statement, print the value of state and
cities_list. print
statement. print
statement. else
clause. else
write an assignment state that
gets the current value associated with state
and assigns it to cities_list. print
statement. for
loop write a statement to return the
cities dictionary. pass
statement from print_cities. print
statement from print_cities. print
statement. print
statement. for
loop over cities_list
using city as the loop variable. for
loop print state
and cities_string. pass
statement from print_city_count. print
statement. for
loop over the parameter dict
using state as the loop variable. for
loop print state and the length of it's
associated value. New York New York City California Los Angeles San Diego San Jose San Francisco Fresno Sacramento Long Beach Oakland Illinois Chicago Texas Houston San Antonio Dallas Austin Fort Worth El Paso Arlington Arizona Phoenix Tucson Mesa .... New York 1 California 8 Illinois 1 Texas 7 Arizona 3 ...
cd 117
cd hw
cd hw4
chmod 755 hw4.py
Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.