IT 117: Intermediate Scripting
Homework 3

Due

Sunday, February 16th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read

class_dictionary_create

print_classes

Test Code

Output

Suggestions


  1. Create the file hw3.py
    Copy the function headers for open_file_read, class_dictionary_create and print_classes into it.
    For the body of each function enter pass.
    Make the script executable and run it.
    You should see nothing.
    If you get an error message, fix the problem before proceeding.
  2. Remove the pass statement from open_file_read.
    In its place write a try/except statement.
    The try block should contain code to create a file object for reading using the using the filename parameter.
    Under this statement return the file object.
    In the except clause print an error message.
    Copy the text code to the bottom of the file.
    Comment out the last two lines.
    Run the script.
    You should see
    Cannot open xxxxx
  3. Remove the pass statement from class_dictionary_create.
    Create the empty dictionary class_dict.
    Write a for loop that prints each line in the file.
    Remove the comment from the 3rd line in the test code.
    Run the script and fix any errors find.
  4. Remove the print statement you just created.
    In it's place set the variable fields to what you get when you call split on the line.
    Print fields.
    Run the script and fix any errors.
  5. Remove the print statement you just created.
    Define the variable class_id and set its value to the first element of the list fields.
    Print the value of class_id.
    Run the script and fix any errors.
  6. Remove the print statement.
    Create the variables room, instr and start from fields using an appropriate index for each.
    Create the variable class_data and set its value to a tuple consisting of three variables defined above.
    To create a this tuple, put parentheses ( ) around these variables.
    Print class_id and class_data.
    Run the script and fix any errors.
  7. Remove the print statement you created above.
    Replace it with a statement that creates an entry in the dictionary class_dict.
    Use class_id as the key and class_data as the value.
    Outside the loop but still inside the function, print this dictionary.
    Run the script and fix any errors.
  8. Remove the print statement from class_dictionary_create.
    Replace it with a statement that returns the dictionary class_dict.
    Remove the pass statement from print_classes and replace it with a statement that prints the parameter class_dict.
    Uncomment the last line in the test code.
    Run the script and fix any errors.
  9. Write a for loop on the sorted dictionary class_info using class_id as the loop variable.
    Inside the loop print class_id.
    Run the script and fix any errors.
  10. Remove the print statement.
    Replace it with a statement that creates the variable class_data from the value in the dictionary specified by the key class_id.
    Print class_id and class_data.
    Run the script and fix any errors.
  11. Change the print statement so that it prints the class_id and each element of the tuple with a Tab, \t, between each value.
    You will have to do this using concatenation.
    Run the script and fix any errors.
  12. Write a print statement before the for loop that print labels for each column.
    Use tabs, \t, to separate each label so it lines up with each value printed in the for loop.
    Write another print statement to produce a line of dashes.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

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