IT 117: Intermediate Scripting
Homework 2
Due
Sunday, February 9th 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 red_sox.txt from
/home/ghoffman/course_files/it117_files.
Use FileZilla to do this.
Specification
Functions
open_file_read
team_average
Test Code
Output
- The output should look something like this
Cannot open xxxxxxx
76
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 script hw2.py using
the editor built into IDLE.
Enter the header for open_file_read and
team_average into the script.
Under each header write the Python statement pass
.
Copy the test code above into the script.
Enter # before the last two lines in the
test code.
Make the script executable.
Run the script.
You should see nothing.
-
Remove the
pass
statement from
open_file_read.
In its place write a try/except
statement.
The try block should contain code to create a file object for
reading using the using the
filename parameter.
Under this statement return the file object.
In the except
clause print an error message.
Run the script.
You should see
Cannot open xxxxxxx
-
Remove the
pass
statement from
team_average.
Create the variable line_count and set it to 0.
Create the variable win_count and set it to 0.
Now write a for
loop that prints each line in the file.
Remove the # from the 2nd to last line of the
test script.
Run the script.
Fix any errors you find.
-
Remove the print statement.
Replace it with a statement that adds 1 to
line_count.
Write an if
statement that checks that the line
contains "Win".
If it does, add 1 to win_count.
Outside the for
loop print
win_count.
Run the script.
You should see
Cannot open xxxxxxx
19
Fix any errors you find.
-
Remove the print statement.
Replace it with a line that sets average
to win_count divided by
line_count.
Print line_count.
Run the script.
You should see
Cannot open xxxxxxx
0.76
Fix any errors you find.
-
Delete the print statement.
Replace it with a statement that returns 100 times the rounded value
of average.
Remove the # from the last
line of the test code.
Run the script.
Fix any errors you find.
Testing on Your Machine
- Open IDLE
- Use the Open command in IDLE to open hw2.py
- Under the Run menu, select Run Module
- Your output should look something like this
Cannot open xxxxxxx
76
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 red_sox.txt
from the bottom left panel to the bottom right panel
Testing the Script on Unix (Optional)
- Connect to pe15.cs.umb.edu
using an ssh client like putty.exe (Windows)
or ssh (Mac)
- Go to the directory for this exercise
cd it117/hw/hw2
- Run this script
python3 hw2.py
- You should see something like this
Cannot open xxxxxxx
76
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.