IT 117: Introduction to Scripting
Homework 2
Due
Sunday, September 15th 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
- average_score
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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
- Open IDLE.
- Use the Open command in IDLE to open hw2.py
- Run your script inside IDLE
- Your output should look something like this
Average 84
Copy the file to Unix
- Open FileZilla and connect to
pe15.cs.umb.edu
You will have to connect using your Unix username and password.
- Go to your 117 directory
- Go to your hw directory
- Inside this directory create the directory hw2
- Copy your hw2.py file from your machine to the
hw2 directory on pe15
Make the File Executable
- Connect to pe15.cs.umb.edu
Use an ssh client.
- Go to your it117 directory
cd 117
- Go to your hw directory
cd hw
- Go to your hw2 directory
cd hw2
- Change the permissions on the script
chmod 755 hw2.py
Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.