IT 117: Intermediate to Scripting
Homework 4

Due

Sunday, September 29th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


cities_dictionary_create


print_cities


print_city_count


Test Code

Output

Suggestions

  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 Script to Unix

Testing the Script on Unix (Optional)

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