IT 117: Introduction to Scripting
Homework 2

Due

Sunday, February 5th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read

highest_score

Test Code

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)

Output

Suggestions

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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

Testing the Script on Unix (Optional)

Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.