IT 117: Intermediate Scripting
Due Sunday, September 16th at 11:59 PM
There is one deliverable for this assignment:
It must be in an hw2 directory, which you must
create inside a hw directory inside you
Make sure the script obeys all the rules in the Script Requirements page.
To test this script you must copy into your hw2 directory
the file red_sox.txt from
To do this go to your hw2 directory and run
cp /home/ghoffman/course_files/it117_files/red_sox.txt .
Create the function
team_average which has the following header
This function will return the average number of games won by the Red Sox from a text file
with entries like this
2011-07-02 Red Sox @ Astros Win 7-5
2011-07-03 Red Sox @ Astros Win 2-1
2011-07-04 Red Sox vs Blue Jays Loss 7-9
This function should create a file object for the file whose name is given by the
If the file cannot be opened, use a
try/except statement to print an error message
and don't do any further work on the file.
The function will count the number of lines and the number of games the Red Sox won and use
these values to compute the average games won by the team.
This average should be expressed as an integer.
Your hw2.py file must contain the following test code at the bottom of the file
Write this program in a step-by-step fashion using the technique of
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 script hw2.py using
nano or some
other Unix text editor.
Enter the header for team_average into the script.
Under this header write the Python statement
Copy the test code above into the script.
Make the script executable.
Run the script.
If all you see is
None, proceed to the text step.
- Remove the
In its place write the function code to open the file for reading.
This code should print an error message and exit the function if the file cannot be opened for reading.
Run the script.
You should see
Cannot open xxxxxxx
- Modify the function so it prints out the lines of the file.
- Modify the function to use the split string method
to create a list from the from the different fields in a line.
Print this list.
- For each line, get the value of the won_lost field and print it.
This field is second to last entry in the list you created by using the split
The lists will have different lengths, since some team have a single word as their name,
and others have two words, e.g. White Sox.
The easiest way to get the second to last field is to use a negative index.
Print the value of won_lost field.
- Comment out or delete the line that prints the won_lost field.
Initialize an accumulator
to count the number of lines.
Increment this accumulator inside the
Return the total number of lines.
- Initialize an accumulator to count the number of Red Sox wins.
for loop increment this accumulator every time
the value of won_lost is "Win".
Return the number of games won.
- Change the
return statement so it returns integer percent
of the number of games the Sox won.
When you run the completed script, you should see
Error: Unable to open xxxxxxx
Be sure to run this script on the Unix machine so you know it works
in the environment in which I will run it when I score your homework.