Exercise8

Write an algorithm in python that returns the average of the terms in a list of numbers.

Solution

def averageList (L):
# initialization of the average
m = 0
for x in L:
m = m + x
# average = sum of the elements of L / length of L
m = m / len(L)
return m

# Example
L = [3, 7, 8, 2]
print ("the average is: m =", averageList (L))
# the output is: the average is: m = 5.0

 

Younes Derfoufi
my-courses.net

Leave a Reply