Question: Deliverables There is one deliverable for this assignment hw0.py Make sure the script obeys all the rules in Homework Script Rules Specification This script must

Deliverables

There is one deliverable for this assignment

  • hw0.py

Make sure the script obeys all the rules in Homework Script Rules

Specification

This script must 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 have 3 functions:

  • open_file_read
  • state_dictionary_create
  • print_dictionary

open_file_read

This function must have the following header:

def open_file_read(filename):

It must try to create a file object for reading on the file whose name is given by the parameter filename.

If it is succesful in creating the file object it should return the object.

If it is cannot create the object it should print an error message and return None.

state_dictionary_create

This function must have the following header:

def state_dictionary_create(file):

The function must read in a file using the file object file.

It must read the file and create dictionary where state names are the keys and the values are population.

The population must be stored as an integer.

The function must return the dictionary it creates.

print_dictionary

This function must have the following header:

def print_dictionary(dict):

The function must print the key and value for each entry in the dictionary sorted by key.

Script for this assignment

Open an a text editor and create the file hw3.py.

You can use the editor built into IDLE or a program like Sublime.

Test Code

Your hw0.py file must contain the following test code at the bottom of the file:

filename = input("File name: ") file = open_file_read(filename) if file: state_populations = state_dictionary_create(file) if state_populations: print_dictionary(state_populations)

For this test code to work, you must copy state_populations.txt to your machine.

To do this use FileZilla to copy the file state_populations.txt from /home/ghoffman/course_files/it117_files into the directory that holds your hw3.py script.

Run the script entering state_populations.txt when prompted.

You should see

$ ./hw0.py Alabama 4903185 Alaska 731545 Arizona 7278717 Arkansas 3017825

Suggestions: follow these instructions

Write this program in a step-by-step fashion using the technique of incremental development.

In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.

  1. Create the file hw0.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.
  2. Replace the pass statement in open_file_read with the body of the code from your hw2.py script. Copy the first two lines of the test code into the bottom of the file. Run the script entering both a real filename and the name of a file that does not exists. Fix any errors you find.
  3. Replace the pass statement in state_dictionary_create with a for loop that prints each line in the file. Copy the next two lines of the test code to the bottom of the file. Run the script entering full_student_data.txt when prompted. Fix any errors you find.
  4. 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. Print the values of state and population. Run the script. Fix any errors you find.
  5. Remove the print statement. Before the for loop create an empty dictionary. Inside the for loop create an entry in the dictionary use the values in state and population. Be careful about data types. After the for loop return the dictionary. Run the script. Fix any errors you find.
  6. Remove the pass statement from print_dictionary. Replace it with a line that prints the value of the parameter, dict. Add the last two lines of the test code. Run the script. Fix any errors you find.
  7. Remove the print statement, Replace it with a for loop that prints 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

  • Open IDLE
  • Use the Open command in IDLE to open hw3.py
  • Under the Run menu, select Run Module
  • Your output should look something like this
    $ ./hw00.py File name: state_populations.txt Alabama 4903185 Alaska 731545 Arizona 7278717 Arkansas 3017825
    The text in bold must be entered at the command line.

State_population flie:

California,39512223 Texas,28995881 Florida,21477737 New York,19453561 Illinois,12671821 Pennsylvania,12801989 Ohio,11689100 Georgia,10617423 North Carolina,10488084 Michigan,9986857 New Jersey,8882190 Virginia,8535519 Washington,7614893 Arizona,7278717 Massachusetts,6949503 Tennessee,6833174 Indiana,6732219 Missouri,6137428 Maryland,6045680 Wisconsin,5822434 Colorado,5758736 Minnesota,5639632 South Carolina,5148714 Alabama,4903185 Louisiana,4648794 Kentucky,4467673 Oregon,4217737 Oklahoma,3956971 Connecticut,3565287 Utah,3205958 Iowa,3155070 Nevada,3080156 Arkansas,3017825 Mississippi,2976149 Kansas,2913314 New Mexico,2096829 Nebraska,1934408 West Virginia,1792147 Idaho,1787065 Hawaii,1415872 New Hampshire,1359711 Maine,1344212 Montana,1068778 Rhode Island,1059361 Delaware,973764 South Dakota,884659 North Dakota,762062 Alaska,731545 DC,705749 Vermont,623989 Wyoming,578759

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!