IT 116: Introduction to Scripting
Homework 9

Due

Sunday, April 7th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

average_score

	set count to 0
	set total to 0
	for each line in the file:
		get the values for id and score
		turn score into an integer
		add score to total
		increment count by 1
	find the average by dividing total by count
	return the average using round to turn it into an integer
	

highest_lowest_score

	set max_score to 0
	set min_score to 100
	for each line in the file:
		get the values for id and score
		turn score into an integer
		if score is greater than max_score:
			set max_score to score
		if score is less min_score:
			set min_score to score
	return max_score, min_score

Test Code

Output

Suggestions


  1. Create a hashbang line on the first line of the script.
    Copy only the function headers for the two functions listed above into your script file.
    Under each header, write pass.
    Make sure this statement is indented.
    Run the script.
    Fix any errors you find.
  2. Remove the pass from average_score.
    Create the variables count and total giving them initial values of 0.
    Print the variables total and count.
    Copy all of the test code to the bottom of your script.
    Comment out all but the first three lines of the test code.
    Do this by inserting # at the beginning of the line.
    Run the script.
    Fix any errors you find.
  3. Remove the print statement from average_score.
    In it's place write a for loop that prints every line in the file.
    Run the script.
    Fix any errors you find.
  4. Remove the print statement.
    Replace it with a statement that calls split on the line to assign values to the variables id and score.
    Print id and score.
    Run the script.
    Fix any errors you find.
  5. Remove the print statement.
    Replace it with an assignment statement that turns score into an integer using int().
    Next increment count.
    Write a statement that adds score to total.
    Outside the for loop, print count and total.
    Run the script.
    Fix any errors you find.
  6. Remove the print statement.
    Replace it with a statement that calculates the average and assigns it to the variable average.
    The value of average is a float.
    Use an assignment statement to make average an integer using round.
    Return average.
    Uncomment the first commented line in the test code.
    Run the script.
    Fix any errors you find.
  7. Remove the pass from highest_lowest_score.
    Create the variable max_score and give it the value of 0.
    Create the variable min_score and give it the value 100.
    Print the variables max_score and min_score.
    Uncomment the next three lines in the test code.
    Run the script.
    Fix any errors you find.
  8. Remove the print statement from highest_lowest_score.
    In it's place write a for loop that prints every line in the file.
    Run the script.
    Fix any errors you find.
  9. Remove the print statement.
    Replace it with a statement that calls split on the line to assign values to the variables id and score.
    Print id and score.
    Run the script.
    Fix any errors you find.
  10. Remove the print statement.
    Replace it with an assignment statement that turns score into an integer using int().
    Write an if statement that will run if score is greater than max_score.
    Inside this if statement set max_score to score.
    Also inside the if statement print max_score.
    Run the script.
    Fix any errors you find.
  11. Remove the print statement.
    After the 1st if statement write another if statement that will run if score is less than min_score.
    Inside this if statement set min_score to score.
    Also inside the if statement print min_score.
    Run the script.
    Fix any errors you find.
  12. Remove the print statement.
    Outside the for loop return max_score and max_score.
    Uncomment the last line in the test code.
    Run the script.
    Fix any errors you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

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