Description
Objectives
Work with lists
Work with functions
Work with files
Assignment
Part 1
Write a program to ask for a series of floating point numbers. Continue to ask for numbers until
the user enters -1. Add each valid value to a list. Once the user has finished entering data ask
for a file name. You may use input or FileUtils to ask for the file name. Write out each
number to the data file, one number per line. Make sure to end each line with a newline (“\n”)
character.
Part 2
Write each of the following functions. The function header must be implemented exactly as
specified. Write a main function that tests each of your functions.
Specifics
In the main function ask for a filename from the user and call fillListFromFile. This will
return a list with the values from the file. Each file should have one numeric value per line. Do
not create this data file in this program. Then call each of the required functions and then
display the results returned from the functions. Remember that the functions themselves will not
display any output, they will return values that can then be written to the screen.
If there are built in functions in Python that will accomplish the tasks lists below YOU CANNOT
USE THEM. The purpose of this lab is to get practice working with lists, not to get practice
searching for methods. DO NOT sort the list, that changes the order of the items. The order of
the values in the list should be the same as the order of the values in the file.
For each function that returns a numeric value determined from the list, unless otherwise stated
in the function, if you are unable to determine a value return None.
Required Functions
def fillListFromFile (fileName) – Creates a list in the function and fills the list with the
values from the fileName file. Then it returns the list. The file should have one number per line.
No error checking is required.
def findMaxValue (theList) – Return the largest value found in the list.
def findMinValue (theList) – Return the smallest value found in the list.
def calcRange (theList) – Return the range of values found in the list. The range is the
difference between the largest and smallest values. This function MUST USE findMaxValue
and findMinValue to determine the value to return. This function CANNOT have its own loop.
def calcAverage (theList) – Return the average of all the values found in the list.
def calcGeometricMean (theList) – Multiple all of the values in the list together, and then
return the nth root of the product, where n is the number of items in the list. To calculate the nth
root of a value use 1/n as the power in an exponential expression. For example, if a lists
contains 2, 3, and 7 you would multiple 2 * 3 * 7, which returns 42. Then take the 3rd root (n =
3) of 42, which is 3.4760266448864496.
def standardDeviation (theList) – Return the standard deviation of the values. To find the
standard deviation start with the average of the list. You must use calcAverage to find this
average. Then, for each value in the list subtract the average from that value and square the
result. Find the average of the squared differences and then take the square root of that
average.
For example, if the list contains the values 4, 5, 6, and 3 the average of those values would be
4.5. Subtract 4.5 from each value and square that results. The differences would be -0.5, 0.5,
1.5 and -1.5 respectively. The square of each of those differences would be 0.25, 0.25, 2.25,
and 2.25 respectively. The average of these values is 1.25 ((0.25 + 0.25 + 2.25 + 2.25) / 4).
The following is copied from a spreadsheet used to calculate the standard deviation.
4 5 6 3 values
4.5 average
-0.5 0.5 1.5 -1.5 difference from average
0.25 0.25 2.25 2.25 difference squared
1.25 average of “difference squared”
1.118034 square root of above average