IT 117: Introduction to Scripting
Homework 2
Due
Sunday, February 8th 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
lowest_score
- Header
def lowest_score(file):
- This function takes one parameter,
file, a file object for reading
- The function should loop through the file
looking for the lowest score and the name of the student with
that score
- The function must return the lowest score and the name
of the student who earned that score
- The function should use the following algorithm
set min_score to 500
set min_name to the empty string
for each line in the file:
split the line into three values score first and last
concatenate first, a space and last and assign it to name
turn score into an integer
if score is less than min_score:
replace min_score with score
replace min_name with name
return min_score and min_name
Test Code
- Your script must contain the following statements
filename = input("File name: ")
file = open_file_read(filename)
if file:
min_score, min_name = lowest_score(file)
print()
print("Lowest score", min_score, min_name)
- They should appear at the bottom of your script
- 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
lowest_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.
Inside the try block write a statement that creates
a file object for reading.
Then return the file object.
Inside the except block write a statement that prints an error
message.
Then return None.
Copy the test code to the bottom of the file.
Comment out all but the first two lines of the test code.
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
lowest_score.
Write an assignment statement that sets min_score
to 500.
Write another assignment statement that sets min_name
to the empty string.
Write a for loop that prints each line in the file.
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.
-
After the
print statement in
lowest_score, write an assignment statement
with the variables score,
first and last on
the left side and a call to split on the variable
holding the line.
For an example of what I mean, see
Class Exercise 3.
Print the three new variables.
Run the script.
Fix any error you find.
-
Remove the statement that prints the line.
Create the variable name by concatenating
first and last
with a space in between.
Write an assignment statement that converts
score to an integer.
Change the remaining print statement by replacing
first and last with
name.
Run the script.
Fix any error you find.
-
After the statement that converts score
to an integer, write an
if statement that will
will run if the value of score is less
than min_score.
If it is, set min_score to the value
of score and
min_name gets the value of
name.
If you are not sure what I mean see this
Class Exercise
from IT 116.
Run the script.
Fix any error you find.
-
Remove the
print statement from inside the
for.
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.