IT 117: Intermediate Scripting
Homework 2

Due

Sunday, February 9th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


team_average

Test Code

Output

Suggestions

  1. Create the script hw2.py using the editor built into IDLE.
    Enter the header for open_file_read and team_average into the script.
    Under each header write the Python statement pass.
    Copy the test code above into the script.
    Enter # before the last two lines in the test code.
    Make the script executable.
    Run the script.
    You should see nothing.
  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.
    Run the script.
    You should see
    Cannot open xxxxxxx
  3. Remove the pass statement from team_average.
    Create the variable line_count and set it to 0.
    Create the variable win_count and set it to 0.
    Now write a for loop that prints each line in the file.
    Remove the # from the 2nd to last line of the test script.
    Run the script.
    Fix any errors you find.
  4. Remove the print statement.
    Replace it with a statement that adds 1 to line_count.
    Write an if statement that checks that the line contains "Win".
    If it does, add 1 to win_count.
    Outside the for loop print win_count.
    Run the script.
    You should see
    Cannot open xxxxxxx
    19
    Fix any errors you find.
  5. Remove the print statement.
    Replace it with a line that sets average to win_count divided by line_count.
    Print line_count.
    Run the script.
    You should see
    Cannot open xxxxxxx
    0.76
    Fix any errors you find.
  6. Delete the print statement.
    Replace it with a statement that returns 100 times the rounded value of average.
    Remove the # from 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 © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.