IT 116: Introduction to Scripting
Homework 9

Due

Sunday, November 9th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

mean_temp

	set count to 0
	set total to 0
	for each line in the file:
	   increment count
	   get the date and temp
	   turn temp an integer
	   add temp to total
	find the mean by dividing total by count
	return the mean using round to turn it into an integer

max_min_temp

	set max to 0
	set min to 500
	for each line in the file:
	   get the date and temp
	   turn temp an integer
	   if temp is greater than max:
	      set max to temp
	   if temp is less than min:
	      set min to temp
	return max and min

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 mean_temp.
    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 mean_temp.
    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 date and temp.
    Print date and temp.
    Run the script.
    Fix any errors you find.
  5. Remove the print statement.
    Replace it with an assignment statement that turns temp into an integer using int().
    Next increment count.
    Write a statement that adds temp 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 statement from max_min_temp.
    Define the variable max and set it to 0.
    Define the variable min and set it to 500.
    Print max and min.
    Uncomment the next two lines in the test code.
    Run the script.
    Fix any errors you find.
  8. Remove the print statement. Replace it with a statement that uses split on line to assign values to the variables date and temp.
    Print date and temp.
    Run the script.
    Fix any errors you find.
  9. Remove the print statement.
    Replace it with an assignment statement that turns temp into an integer using int().
    Write an if statement that will run if temp is greater than max.
    Inside the if statement write an assignment statement that sets max to the current value of temp. Print max.
    Run the script.
    Fix any errors you find.
  10. Remove the print statement.
    Replace it with an if statement that will run if tempis less than min.
    Inside the if statement write an assignment statement that sets min to the current value of temp. Print min.
    Run the script.
    Fix any errors you find.
  11. Remove the print statement.
    Outside the for loop return max and min.
    Uncomment the last lines 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 © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.