IT 117: Intermediate Scripting
Homework 3

Due Sunday, February 11th at 11:59 PM


There is one deliverable for this assignment: It must be in an hw3 directory, which you must create inside a hw directory inside you it117 directory.

Make sure the script obeys all the rules in the Script Requirements page.

To test this script you must copy into your hw3 directory the file courses.txt from /home/ghoffman/course_files/it117_files.


This script reads in class data from a file and creates a dictionary.

It then prints the data from the dictionary.

The script has the following functions


This function has the following header
def class_tuple_create(filename):
The function reads in a file with four fields of data about a class.

The fields are
	Course ID	Room number	Instructor	Start time
The function must print an error message if the file cannot be opened for any reason.

The function should read in each line of the file and split it into a list of 4 elements.

It should add the information in the line into a dictionary, where the key is the course number and the value is a tuple consisting of the room number, the instructor and the start time.

The function should return this dictionary when it has read all the lines in the file.


This function has the following header
def print_classes(class_info):
It should print the following table header
ID	Room		Instr	Start
Then it should print out the data for each class in alphabetical order by Class ID.

Test Code

The script must contain the following test code at the bottom of the file
class_information = class_tuple_create('xxxxx')
class_information = class_tuple_create('courses.txt')


Write this script in stages, testing your script at each step
  1. Create an file with the hashbang line.
    Now copy the the function headers above into this file.
    If you tried to run this script now you would get an error, because there is no indented code for the two functions.
    Under each function header hit Tab and type the Python statement pass.
    pass does nothing, but it is a legal Python statement so the interpreter will not object.
    Make the code executable.
    Run the script.
    If you see nothing, you have no errors and you can go on to the next step.
    If you get an error message, fix the problem before proceeding.
  2. Copy the test code above into your script.
    Run the script.
    You should see two error messages that tell you files cannot be opened.
    If you have any errors, fix them.
  3. Remove the pass statement in class_tuple_create.
    This function will need to open a file so you will have to write a try/except statement.
    Create a file object so you can read the file given as a parameter.
    Write an error message if the file cannot be opened.
    Run the script.
    You should see one error message.
  4. Using a for loop print each line of the file.
    Run the script and fix any errors.
  5. Remove the print statement you just created.
    In it's place use the split string method to create a list of words in the line.
    Print the list of words.
    Run the script and fix any errors.
  6. Remove the print statement you just created.
    Define the variable course_id and set its value to the first element of the list.
    Print the value of this variable.
    Run the script and fix any errors.
  7. Create the variable class_data and set its value to the tuple consisting of the remaining fields in the list.
    Print course_id and class_data.
    Run the script and fix any errors.
  8. Create an empty dictionary at the top of the function.
    With each line of the file create an entry for the class where the key is the course ID and the value is the tuple. Outside the loop but still inside the function, print this dictionary.
  9. Remove all print statements from class_tuple_create. Return the dictionary created by this function. Remove the pass statement from print_classes and replace it with a statement that prints the parameter.
  10. Using a for loop, print the class IDs in sorted alphabetical order.
  11. Use the class ID to get the tuple with class data. Print the class ID and tuple for each class.
  12. Print the class ID and each element of the tuple with a tab in between.
  13. Print the labels for each column and a line of dashes underneath it.


The output should look something like this

Error: Unable to open xxxxx
ID  Room    Instr   Start
CM241   1411    Lee 13:00
CS101   3004    Haynes  8:00
CS102   4501    Smith   9:00
CS103   6755    Rich    10:00
NT110   1244    Burke   11:00