IT 117: Introduction to Scripting
Homework 4
Due
Sunday, September 28th at 11:59 PM
What You Need to Do
- Create the script hw4.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 hw4
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 hw4.py
- Copy the file
cities_counties_cases.txt from
/home/ghoffman/course_files/it117_files.
Use FileZilla to do this.
Specification
- This script reads file with 3 values
- city
- County
- Number of cases
on each line
- The file looks like this
Barnstable,Barnstable,1
Bourne,Barnstable,5
Brewster,Barnstable,9
...
- It should create a dictionary where the county is the
key and the total number of cases for the country is the value
- The script must contain 3 functions
- open_file_read
- cases_dictionary_create
- highest_cases
- The script should print the name of the county with the
highest number of cases along with the total cases
Functions
open_file_read
cases_dictionary_create
highest_cases
Test Code
- Your script must contain the following statements
- They should appear at the bottom of your script
filename = input("File name: ")
file = open_file_read(filename)
if file:
cases = cases_dictionary_create(file)
max_county, max_cases = highest_cases(cases)
print(max_county,max_cases)
- This code will only work if you have copied
cities_counties_cases.txt into the same directory
as your script
- You will find this file in
/home/ghoffman/course_files/it117_files
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 hw4.py.
Enter the headers for open_file_read,
cases_dictionary_create and
highest_cases.
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.
Copy the test code to the bottom of the file.
We want the first two lines to run now.
To make this happen, we need to comment out all but the first two
lines of the test code.
Run the script entering both a real filename and the name of a file
that does not exists.
Fix any errors you find.
-
Remove the
pass
statement from
cases_dictionary_create.
Create the empty dictionary county_cases.
Write a for
loop that prints every line in the file.
Uncomment the next two lines of the test code.
Run the script entering cities_counties_cases.txt
when prompted.
Fix any errors you find.
-
Remove the
print
statement.
Use the split string method
on each line in the file to assign values to the variables
city, county and
cases.
You will have to give split
the argument
"," since a comma appears between each
value on the line.
Use either one of the techniques found in
Class Exercise 2 or
Class Exercise 3
to assign values to the variables.
Write and assignment statement that converts cases
to an integer.
Print the value of the three variables.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
When the script comes across a value for
count that it has not seen before,
it needs to create an entry for this new key.
That entry will be the number contained in
cases.
Write an if
statement that checks whether the
the value of county is already in
county_cases.
If it is, create a new entry in county_cases
with county as the key and
cases as the value.
Outside the for loop print the dictionary
county_cases.
Run the script.
Fix any errors you find.
-
Now we have to deal with the situation where the value for
county is already in the dictionary.
In cases_dictionary_create, continue the
if
statement with an else
clause.
In the code block for this new clause, get the current value
for cases associated with the
county and assign it to the variable
current_casses.
Under this write a statement that sets the new value in
the dictionary for country to
be the sum of cases and
current cases.
Run the script.
Fix any errors you find.
-
We need to finish the work on
cases_dictionary_create so we can proceed
to highest_cases.
All we have to do here is replace the print
statement
with a statement that returns the dictionary
county_cases.
Now go to highest_cases and remove the
pass
statement.
In it's place write a for
loop that loops through the
entries in the dictionary.
Inside the loop print the key and value for each entry.
Run the script.
Fix any errors you find.
-
Now you are going to have to find the county with the highest number
of cases.
You should use the same technique used in
Class Exercise 3.
Above the for
loop assign the variable
max_cases the value 0 and assign
max_county the empty string.
Remove the print
statement inside the for
loop.
Write and if
statement that checks whether the value of
cases is greater than
max_cases.
If it is, set max_caes to the value of
cases and max_county
to county.
Outside the for loop return
max_county and max_cases.
Uncomment the last line of the test code.
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 hw4 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
cities_counties_cases.txt from the
bottom left panel to the bottom right panel
Testing the Script on Unix (Optional)
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.