IT 117: Introduction to Scripting
Homework 4

Due

Sunday, September 28th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


cases_dictionary_create


highest_cases

Test Code

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)

Output

Suggestions

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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

Testing the Script on Unix (Optional)

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