In: Computer Science
Implement the following function in the PyDev module functions.py and test it from a PyDev module named :
def statistics(n): """ ------------------------------------------------------- Asks a user to enter n values, then calculates and returns the minimum, maximum, total, and average of those values. Use: minimum, maximum, total, average = statistics(n) ------------------------------------------------------- Parameters: n - number of values to process (int > 0) Returns: minimum - smallest of n values (float) maximum - largest of n values (float) total - total of n values (float) average - average of n values (float) ------------------------------------------------------ """
Sample testing:
Enter number of values: 5 First value: 1 Next value: 2 Next value: -3.4 Next value: 4 Next value: 5.3 Minimum: -3.40 Maximum: 5.30 Total: 8.90 Average: 1.78
def statistics(n): # function definition starts
numbers = [] # created a list to store numbers
for i in range (n):
numbers.append(float(input("Enter the number: "))) # append values to list
i = 0
maximum = numbers[0] # initialized minimum to 1st value in the list
minimum = numbers[0] # initialized maximum to 1st value in the list
total = 0 # to calculate sum
for i in range(n):
if (numbers[i]>maximum):
maximum = numbers[i]
if (numbers[i]<minimum):
minimum = numbers[i]
total += numbers[i] # sum calculated
print("Sum of numbers is {}.".format(total))
print("Average of the numbers is {}.".format(total/n)) # total/n = average
print("Minimum value is {}".format(minimum))
print("Maximum value is {}".format(maximum))
number_of_inputs = int(input("Enter the number of values you want to enter: ")) # taking input
statistics(number_of_inputs) # calling function
OUTPUT: