Python 1: Introduction to Python
Homework 10
Due Sunday
Deliverables
There is one deliverable for this assignment:
Specification
The script must define 4 functions
- read_file_into_integer_list
- list_mean
- list_median
- list_range
You must not use any external Python module in the statistical calculations
needed for these functions.
read_file_into_integer_list
This functions must have the following header
def read_file_into_integer_list(filename):
This function must read in the numbers contained in
a file, convert them to integers and add them to an array.
The function must return the array of integers it creates.
If the function cannot open the file, it should return an empty list.
list_mean
This functions must have the following header
def list_mean(list):
This function must calculate the average (mean) of
the integers in a list.
It must return the the rounded average.
list_median
This functions must have the following header
def list_median(list):
This function must sort a list and return the value
of the element in the middle of sorted the list.
You may assume that the list has an odd number of elements.
list_range
This functions must have the following header
def list_range(list):
This function must return the difference between the
highest and lowest number in a list.
Suggestions
- Create the code file and make it executable.
Run the code.
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
.
Copy the test code below to the bottom of your script.
Run the script.
All the values should be None
.
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 as a parameter
to this function.
The except
code block should return list.
Run the script and fix any errors you find.
- Add an
else
clause to the try/except
statement.
Inside this clause add a for
loop that reads each
line in the file and prints it.
Run the script and fix any errors you find.
- Change the body of the
for
loop so it converts
each line of the file into an integer and adds it to list.
Outside the for
loop, return list.
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 on the file given as a parameter which
prints each number in the list.
Run the script and fix any errors you find.
- Replace the print statement in the
for
loop with a statement
that adds the value of the number to total.
Outside the for
loop, return the rounded value of
total divided by the length of the list.
Run the script and fix any errors you find.
- Remove the
pass
statement from list_median.
Sort the 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.
- Remove the
pass
statement from list_range.
Sort the list.
Return the difference between the last and first elements of the list.
Run the script and fix any errors you find.
Test Code
At the bottom of the code you must have the following code
numbers = read_file_into_integer_list("xxxxxxx")
numbers = read_file_into_integer_list("numbs.txt")
print("numbers: ", numbers)
print("list_mean(numbers): ", list_mean(numbers))
print("list_median(numbers):", list_median(numbers))
print("list_range(numbers): ", list_range(numbers))
Testing
Your output should look like this
numbers: [46, 19, 35, 43, 35, 33, 42, 6, 25, 6, 19, 17, 31, 1, 23, 14, 18, 24, 24]
list_mean(numbers): 24
list_median(numbers): 24
list_range(numbers): 45