IT 117: Introduction to Scripting
Homework 4
Due
Sunday, February 19th 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 first two lines of the test code into the bottom of the file.
Run the scrip 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.
Copy the next two lines of the test code into your script.
Run the script entering cities_counties_cases.txt
when prompted.
Fix any errors you find.
-
Remove the
print
statement.
You will have to use the split string method
on each line in the file to assign values to the variables
city, county and
cases, but you need to use "," as the
argument to split.
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.
Now you are going to have to create entries in the dictionary
county_cases, but this will take some thinking.
You need to write an if
statement that checks whether the
value of county is already in the dictionary.
If it is add the cases for this entry to the current value of cases in the
dictionary and store that as the new value for county.
If it is not already in the dictionary, create a new value entry 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.
-
Remove the
print
statement.
In it's place write a statement to return
county_cases.
Remove the pass
statement from
highest_cases.
In it's place write a for
loop that loops through the
entries in the dictionary.
Inside the loop get the value of the current entry and assign
it to the variable cases.
Print county and cases
for each entry.
Outside the for loop return two empyy strings.
If you don't you will get a runtime error when you run the script.
Add the next line in the test code to the bottom of the script.
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.
Copy the last line of the test code into the script.
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.