IT 117: Intermediate to Scripting
Homework 4

Due

Sunday, February 18th at 11:59 PM

Deliverables

There is one deliverable for this assignment

Make sure the script obeys all the rules in Homework Script Rules

Specification

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:

open_file_read

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.

cities_dictionary_create

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

print_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

print_city_count

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

Script for this assignment

Open an a text editor and create the file hw4.py.

You can use the editor built into IDLE or a program like Sublime.

Test Code

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.

Suggestions

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.

  1. Create the file hw4.py.
    Enter the headers for each of the required functions.
    Under each header write the Python statement pass.
    Run the script.
    Fix any errors you find.
  2. Replace the pass statement in open_file_read with the body of the code from your hw3.py script.
    Copy the test code into the bottom of the file.
    Run the script.
    Fix any errors you find.
  3. Remove the pass statement from cities_dictionary_create.
    Replace it with a statement that creates an empty dictionary named cities.
    Write a for loop that loops through the file object file using line as the loop variable.
    Inside the for loop use the strip method to remove the linefeed character from .
    You will have to use an assignment statement to do this since strings are immutable.
    Now print line.
    Run the script.
    Fix any errors you find.
  4. Remove the print statement.
    Now you are going to have to get the values for city and state.
    You will do this using the split method on the string line.
    Write an assignment statement that uses split to give values to the variables city and state .
    For an example of what I mean, see Class Exercise 3.
    You will have to give an argument to split.
    Print city and state.
    Run the script.
    Fix any errors you find.
  5. Remove the print statement.
    Write an if statement that runs if state is not already in the cities dictionary.
    You can use the in operator on cities to do this.
    In the body of this if statement, create the empty list cities_list.
    cities_list will contain the list of cities for each state.
    Now write a statement that adds city to cities_list.
    Outside the if statement, print the value of state and cities_list.
    Run the script.
    Fix any errors you find.
  6. Remove the print statement.
    Make an entry in the cities dictionary using state as the key and cities_list as the value.
    Print state and the value associated with it in cities .
    Run the script.
    Fix any errors you find.
  7. Remove the print statement.
    The code you have written so far made an entry in cities each time an new value for state is read from the file.
    Now we need to write code to add a new city to and existing entry for state.
    You will do this with an else clause.
    Inside this clause we need to get the current cities list associated with state and add the new value for city to this list.
    Under else write an assignment state that gets the current value associated with state and assigns it to cities_list.
    Write a statement to add city to cities_list using the append method.
    Now update the value associated with state in the cities dictionary.
    Print state and the value associated with it in cities .
    Run the script.
    Fix any errors you find.
  8. Remove the print statement.
    Outside the for loop write a statement to return the cities dictionary.
    Remove the pass statement from print_cities.
    Replace it with a statement that prints the value of its parameter dict.
    Run the script.
    You should see the entire cities dictionary.
    Fix any errors you find.
  9. Remove the print statement from print_cities.
    Write a for loop, using state as the loop variable.
    Inside the loop, print state and its value.
    Run the script.
    Fix any errors you find.
  10. Remove the print statement.
    Write an assignment statement that stores the value associated with state in the variable cities_list.
    Print state and cities_list.
    Run the script.
    Fix any errors you find.
  11. Remove the print statement.
    Create the empty string cities_string.
    Write a for loop over cities_list using city as the loop variable.
    Inside the loop add each value of city to cities_string along with a space.
    After the for loop print state and cities_string.
    Run the script.
    Fix any errors you find.
  12. Remove the pass statement from print_city_count.
    Print the parameter dict.
    Run the script.
    Fix any errors you find.
  13. Remove the print statement.
    Replace it with a for loop over the parameter dict using state as the loop variable.
    Inside the for loop print state and the length of it's associated value.
    Run the script.
    Fix any errors you find.

Testing on Your Machine

Copy the file to Unix

Make the File Executable

Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.