IT 116: Introduction to Scripting
Homework 11
Due
Sunday, November 24th at 11:59 PM
What You Need to Do
- Create the script hw11.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Make sure the script has a hashbang line and is executable
- Move it to an an hw11
directory on pe15.cs.umb.edu
Setup On Your Machine
- Open a text editor.
I would suggest the text editor built into the program IDLE
.
- Save the file as hw11.py
- Copy the file numbs.txt from
/home/ghoffman/course_files/it116_files.
Use FileZilla to do this.
Specification
- This script will read in a file of numbers, one
to a line and convert it into a list
- The file looks like this
46
19
35
43
...
- It will then find the mean (average) and the median of the numbers in
the list
- The median is the value in the middle of a sorted list
- It will do this by defining 3 functions
- read_file_into_integer_list
- list_mean
- list_median
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
- Header
def list_mean(list):
- This function should use a
for
loop to total all the numbers in the list pointed to
by the parameter list
- It should calculate the average by dividing the
total by the length of the list
- And return the rounded average
- The function should use the following algorithm
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
- Header
def list_median(list):
- This function should sort the list pointed to by
the parameter list
- And return the value in the middle of the list
- The function should use the following algorithm
sort the list
set index to integer division of the length of the list by 2
return index
Test Code
- Your script must contain the following statements
numbers = read_file_into_integer_list("xxxxxxx")
print("numbers: ", numbers)
numbers = read_file_into_integer_list("numbs.txt")
print("numbers: ", numbers)
mean = list_mean(numbers)
print("mean: ",mean)
median = list_median(numbers)
print("median: ", median)
- They should appear at the bottom of your script
Output
- The output should look something like this
numbers: []
numbers: [46, 19, 35, 43, 35, 33, 42, 6, 25, 6, 19]
mean: 28
median: 33
Suggestions
- Write this script in stages
- Test your script at each step
- Print the steps below
- And check them off as you finish each one
-
Create the script file.
Add a hashbang line.
Run the script.
It should produce no output.
Fix any errors you find.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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
- Open IDLE
- Use the Open command in IDLE to open
hw11.py
- Run your script inside IDLE
- Your output should look something like this
numbers: []
numbers: [46, 19, 35, 43, 35, 33, 42, 6, 25, 6, 19]
mean: 28
median: 33
Copy the Script to Unix
- Open FileZilla and connect to
pe15.cs.umb.edu
- Go to your it116 directory
- Go to your hw directory
- Right-click in the whitespace inside the
hw directory
- Enter hw11 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
- Right-click on the file and select "Permissions" from
the menu
- Enter 755 in the box provided
- This will make the script executable
- Click and drag numbs.txt
from the bottom left panel to the bottom right panel
Testing the Script on Unix (Optional)
- Connect to pe15.cs.umb.edu
using an ssh client like putty.exe (Windows)
or ssh (Mac)
- Go to the directory for this exercise
cd it116/hw/hw11
- Run this script
python3 hw11.py
- You should see something like this
numbers: []
numbers: [46, 19, 35, 43, 35, 33, 42, 6, 25, 6, 19]
mean: 28
median: 33
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.