IT 117: Introduction to Scripting
Homework 3

Due

Sunday, February 15th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


state_dictionary_create

	create an empty dictionary
	for each line in the file
		get values for state name and population
		convert population into an integer
		create a dictionary entry with the state name as the key and population the value
	return the dictionary
	

print_dictionary

Test Code

Output

Suggestions

  1. Create the file hw3.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 hw2.py script.
    Copy the test code into the bottom of the file.
    Comment out all but the first two lines.
    Run the script entering both a real filename and the name of a file that does not exists.
    Fix any errors you find.
  3. Replace the pass statement in state_dictionary_create with a statement that creates the empty dictionary state_pop.
    Write a for loop that prints each line in the file.
    Uncomment the next two lines in the test code.
    Run the script entering state_populations.txt when prompted.
    Fix any errors you find.
  4. Remove the print statement.
    Create the list variable fields by calling the split method on each line.
    Use fields to get the values for the variables state and population.
    If you are not sure what I mean, see Class Exercise 2.
    Use an assignment statement to convert population into an integer.
    Print the values of state and population.
    Run the script.
    Fix any errors you find.
  5. Remove the print statement.
    Inside the for loop create an entry in the dictionary using state as the key and population as the value.
    After the for loop return the dictionary.
    Run the script.
    Fix any errors you find.
  6. Remove the pass statement from print_dictionary.
    Replace it with a line that prints the value of the parameter, dict.
    Uncomment the last two lines of the test code.
    Run the script.
    Fix any errors you find.
  7. Remove the print statement,
    Replace it with a for loop that prints the key and value for each entry in the dictionary.
    Remember the entries must be sorted by state.
    Run the script.
    Fix any errors you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

















































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