IT 117: Introduction to Scripting
Homework 4

Due

Sunday, February 19th 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 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.
  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.
    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.
  4. 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.
  5. 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.
  6. 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.
  7. 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

Testing the Script on Unix (Optional)

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