IT 117: Introduction to Scripting
Homework 2

Due

Sunday, September 15th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


average_score


Test Code

Output

Suggestions

  1. Create the file hw2.py.
    Enter the headers for open_file_read and average_score.
    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 code that creates a file object for reading.
    Remember, you will get a runtime error if the file does not exist.
    If the file object is created, the code should return it.
    If not, the code should print an error message and return None.
    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. Remove the pass statement from average_score.
    Initialize the variables total and count to 0.
    After this write a for loop that prints each line in the file.
    Copy the next three lines of the test code to the bottom of the file.
    Run the script entering qz_04_grades.txt when prompted.
    Fix any errors you find.
  4. Replace the print statement in average_score with an assignment statement that uses split to give values to the variables score, first and last.
    For an example of what I mean, see Class Exercise 3.
    Print the two new variables.
    Run the script.
    Fix any error you find.
  5. Remove the print statement.
    In its place write a statement that increased the value of count by 1.
    After the for loop, return the value of count.
    Add the last line from the test code. Run the script.
    Fix any error you find.
  6. Inside the for loop write and assignment statement that converts score into an integer.
    Write a statement that adds score to total.
    Change the return statement to return total.
    Run the script.
    Fix any error you find.
  7. The average is calculated by dividing total by count.
    You will have to round the result to get an integer.
    Change the return statement so it returns the average.
    Run the script.
    Fix any error you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

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