IT 116: Introduction to Scripting
Homework 11

Due

Sunday, November 24th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

read_file_into_integer_list

	create an empty list
	try:
	   create a file object for reading on filename
	   for each line in the file:
	      covert the line into an integer
	      append the integer to the list
	      return the list
	except:
	   return the empty list

list_mean

	set total to 0
	loop over the numbers in the list:
	   add each number total
	set average to the total divided by the length of the list
	return the rounded average

list_median

	sort the list
	set index to integer division of the length of the list by 2
	return index

Test Code

Output

Suggestions


  1. Create the script file.
    Add a hashbang line.
    Run the script.
    It should produce no output.
    Fix any errors you find.
  2. Create a function header for each of the four functions.
    For the body of each function write pass.
    Run the script.
    Fix any errors you find.
  3. Remove the pass statement from read_file_into_integer_list.
    Add a statement that creates an empty list and assigns it to the variable list.
    Within a try/except statement, create a file object on the file whose name is given by the parameter filename.
    The except code block should return list.
    Copy the test code to the bottom of your script.
    Comment out all but the first two lines of the test code.
    Run the script and fix any errors you find.
  4. Add an else clause to the try/except statement.
    Inside this clause write a for that will read each line in the file.
    Inside the loop, print each line.
    Uncomment the next line in the test code.
    Run the script and fix any errors you find.
  5. Remove the print statement.
    Replace it with a statement that turns the line into an integer and assigns the value to the variable num.
    Print num.
    Run the script and fix any errors you find.
  6. Remove the print statement.
    Replace it with a statement that appends num to list.
    Outside the loop, return list.
    Uncomment the next line in the test code.
    Run the script and fix any errors you find.
  7. Remove the pass statement from list_mean.
    Create the variable total and set it to 0.
    Write a for loop over the list pointed to by the parameter list.
    Name the loop variable num.
    Inside the loop print num.
    Uncomment the next line in the test code that calls list_mean.
    Run the script and fix any errors you find.
  8. Remove the print statement.
    Replace it with a statement that adds num to total.
    Outside the for loop print total.
    Run the script and fix any errors you find.
  9. Remove the print statement.
    Replace it with a statement that sets the variable mean to total divided by the length of list.
    Print mean.
    Run the script and fix any errors you find.
  10. Remove the print statement. Replace it with a statement that returns the rounded value of mean. Uncomment the next line in the test code.
    Run the script and fix any errors you find.
  11. Remove the pass statement from list_median.
    Sort the list pointed to by the parameter list.
    Create the variable index and assign it the value of the length of the list divided by 2 using integer division (//).
    Return the value of the list element that has this index.
    Run the script and 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.