Introduction:
This code defines a Python function that calculates the mean of a list of floating point values. The function takes one parameter – a list containing floating point values. It then adds all the numbers in the list and divides the total by the number of elements to compute the mean value. Finally, it prints the calculated mean.
Code:
#Function to calculate mean
#The requirements are listed below:
#1. The function should have 1 parameter (list containing floating point values)
#2. To calculate mean by adding all the numbers and dividing by total number of elements
def myMean(myList): #function to compute means of values in list
total = 0
count = 0
for i in myList:
total = total + i #Adds each element i to total
count = count + 1 #Counts the number of elements
mean = total/count #mean is calculated
print("The calculated mean is:",mean)
myList = [1.3,2.4,3.5,6.9]
#Function call with list "myList" as an argument
myMean(myList)
Logic:
- The function iterates through each element in the list and adds it to a running total.
- It also keeps track of the count of elements.
- After iterating through all the elements, it divides the total by the count to compute the mean value.
- The mean is then printed as output.
Output:
>> The calculated mean is: 3.5250000000000004