IT 117: Introduction to Scripting
Homework 2
Due
Sunday, September 15th at 11:59 PM
What You Need to Do
- Create the script hw2.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Make sure the script has a hashbang line and is executable
- Move it to an an hw2
directory on pe15.cs.umb.edu
Setup On Your Machine
- Open a text editor.
I would suggest the text editor built into the program IDLE
.
- Save the file as hw2.py
- Copy the file qz_04_grades.txt from
/home/ghoffman/course_files/it117_files.
Use FileZilla to do this.
Specification
Functions
open_file_read
average_score
- Header
def average_score(file):
- The function must use the file object given to it by
the parameter file ...
- to loop through the file and calcuate the average score
- It should return the score rounded to an intger
Test Code
Output
Suggestions
- Write this script in stages
- Test your script at each step
- Print the steps below
- And check them off as you finish each one
-
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
Copy the Script to Unix
- Open FileZilla and connect to
pe15.cs.umb.edu
- Go to your it117 directory
- Go to your hw directory
- Right-click in the whitespace inside the
hw directory
- Enter hw2 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
- Right-click on the file and select "Permissions" from
the menu
- Enter 755 in the box provided
- This will make the script executable
- Click and drag qz_04_grades.txt from the
bottom left panel to the bottom right panel
Testing the Script on Unix (Optional)
Copyright © 2024 Glenn Hoffman. All rights reserved. May not be
reproduced without permission.