IT 117: Introduction to Scripting
Homework 2

Due

Sunday, February 8th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


lowest_score

	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

Output

Suggestions

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

Testing the Script on Unix (Optional)

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