In this assignment you are going to use functionsthat were written by someone el
ID: 3811368 • Letter: I
Question
In this assignment you are going to use functionsthat were written by someone else that are contained in the statistics module.For this assignment you are being provided with a pre-written portion of theapplication. That is, part of the code that you need has been written for you. Thestarting point for your application is in the file calcstats.py. It is included with theassignment in a zip file called calcstats.py.zip. The existing code prompts the userfor a file name and loads the numbers in the file into an array. Your job is to writethe code that goes into the calculate_stats(numbers) function in the application.The code you write in calculate_stats is to do the following using the functionsprovided in the Python statistics module and the data presented in the numbersarray:• Calculate the mean.• Calculate the median.• Calculate the median_low.• Calculate the median_high.• Calculate the median_grouped.• Calculate the stdev.• Calculate the variance.• Present the above values to the user using the following before each value:
o mean =
o median =
o median_low =
o median_high =
o median_grouped =
o stdev =
o variance =
code provided:
# Load the numbers from a file into an array.
def load_numbers(filename):
numbers = [] # to hold the list of numbers
try:
number_file = open(filename, "r")
for number in number_file:
number = int(number) # Convert the read string to an int.
numbers.append(number)
except Exception as err:
print ("An error occurred loading", filename)
print ("The error returned was", err)
return numbers
if (len(numbers) < 1):
print ("There were no numbers in", filename)
return numbers
else:
return numbers
# Perform the statistics calculations using the values
# in the numbers array.
def calculate_stats(numbers):
# The main function that contains what the program is to do!
def main():
do_evaluate = True
while(do_evaluate):
filename = input(' What is the file you would like to evaluate? ')
numbers = load_numbers(filename)
if (len(numbers) > 0):
calculate_stats(numbers)
check_evaluate = input(' Would you like to evaluate another file? (y/n) ')
if (check_evaluate != 'y'):
do_evaluate = False
# Call the main() function to make the program do what it is defined to do.
main()
Explanation / Answer
import statistics as s
def calculate_stats(numbers):
mean=s.mean(numbers)
median = s.median(numbers)
median_low =s.median_low(numbers)
median_high =s.median_high(numbers)
median_grouped =s.median_grouped(numbers)
stdev =s.stdev(numbers)
variance =s.variance(numbers)
print("mean=%f"%mean)
print("median=%f"%median)
print("medain_low=%f"%median_low)
print("median_high=%f"%median_high)
print("median_grouped=%f"%median_grouped)
print("stdev=%f"%stdev)
print("varance=%f"%variance)