IT 117: Introduction to Scripting
Homework 3
Due
Sunday, February 15th at 11:59 PM
What You Need to Do
- Create the script hw3.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Make sure the script has a hashbang line and is executable
- Move it to an an hw3
directory on pe15.cs.umb.edu
Setup On Your Machine
- Open a text editor.
I would suggest the text editor built into the program IDLE.
- Save the file as hw3.py
- Copy the file state_populations.txt from
/home/ghoffman/course_files/it117_files.
Use FileZilla to do this.
Specification
- This script will read in a text file containing a list
of states and their populations
- The file has entries like the following
California,39512223
Texas,28995881
Florida,21477737
New York,19453561
...
- The script should create a dictionary where the keys
are the state names and the values are the population
- It should then print the dictionary in alphabetical
order
- The script must contain 3 functions
- open_file_read
- state_dictionary_create
- print_dictionary
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
- Write this script in stages
- Test your script at each step
- Print the steps below
- And check them off as you finish each one
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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
- Open FileZilla and connect to
pe15.cs.umb.edu
- Go to your it117 directory
- Go to your hw directory
- Right-click in the whitespace inside the
hw directory
- Enter hw3 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
- Right-click on the file and select "Permissions" from
the menu
- Enter 755 in the box provided
- This will make the script executable
- Click and drag state_populations.txt from the
bottom left panel to the bottom right panel
Testing the Script on Unix (Optional)
Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.