IT 117: Introduction to Scripting
Homework 2
Due
Sunday, February 5th 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
highest_score
- Header
def highest_score(file):
- This function should use the file object
file to loop throuh the file
to find the hughest score
- It should return the highest score and the name of the
studemt who earned that score
Test Code
- Your script must contain the following statements
- They should appear at the bottom of your script
filename = input("File name: ")
file = open_file_read(filename)
if file:
max_score, max_name = highest_score(file)
print()
print("Highest score", max_score, max_name)
- This code will only work if you have copied
qz_04_grades.txt into the same directory
as your script
- You will find this file in
/home/ghoffman/course_files/it117_files
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
highest_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 a
try
/except
statement.
The try
block should open the file for reading and return
the file object.
The except
block should print an error message and return
None.
Copy the test code to the bottom of the file.
Comment out all but the first two lines of the test code.
You do this by typing a # at the beginning
of each of these lines.
This will prevent these statements from running.
Run the script enteting the filename "xxxx".
You shoul see an error message.
Run the script again entering "qz_04_grades.txt".
Fix any errors you find.
-
Remove the
pass
statement from
highest_score.
Replace it with a for
loop on file
using line as the loop variable.
Inside the loop, print line.
After the for
loop and outside it, return two empty strings.
You need to do this to avoid a syntax error when run the test code.
Uncomment the last 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.
-
Remove the
print
statement inside the loop.
Replace it with an assignment statement that has three variables
score, first and
last on the left side.
On the right side run the split method on
line.
To see what I mean, see
Class Exercise 3.
Print the three new variables.
Run the script.
Fix any error you find.
-
You need to add an assignment statement before the
print
statement inside the for
loop.
This statement will create the variable name
by concatenating first and
last with a space in between.
Change the print
statement by replacing
first and last
with name.
Run the script.
Fix any error you find.
-
Add another assignment statement before the
print
statement.
This statement should change score
to an integer.
On the left side of the = is
score.
On the right, run the int
conversion function on
score.
Run the script.
Fix any error you find.
-
Above the for loop create the variable
max_score and set it to 0.
Next create the variable max_name and set it
to the empty string.
Run the script.
Fix any error you find.
-
Remove the
print
statement inside the for
loop.
Replace it with an if
statement that checks whether
score is greater than
max_score.
If it is, assign the value of score to
max_score and name
to max_name.
If you are not sure what I mean see this
Class Exercise
from IT 116.
Rewrite the return
statement so it returns
max_score and
max_score.
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 © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.