IT 117: Introduction to Scripting
Homework 2

Due

Sunday, February 4th at 11:59 PM

Deliverables

There is one deliverable for this assignment

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

Specification

Your script must read a file with lines like this

95	Alan Ball
98	Eddie Bauer
80	Dan Franks
...

It must calculate the average score and print it.

The score must be an integer.

The script must have two functions

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,

average_score

This function must have the following header

def average_score(file):

The function must use the file object given to it by the parameter file and loop through the file to calculate the average.

The value this function returns must be an integer.

Script for this assignment

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

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

Test Code

Your hw2.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:
    average = average_score(file)
    print()
    print("Average", average)

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

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

Run the script entering qz_04_grades.txt when prompted.

You should see

$ ./hw2.py 
File name: qz_04_grades.txt

Average 84

Suggestions

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 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 file to Unix

Make the File Executable

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