Python-courses-download-Python-courses-s-Python-courses

Exercise 36

Write a Python algorithm as a function which takes as parameter a couple (listScores, listCoefficients) and which returns the average obtained, where listScores designates the list of scores obtained by a students and listCoefficients designates the list of associated coefficients

Solution

def average (listScores, listCoefficients):
# initialization of the mean
average = 0
# sum of the coefficients initialized
sumCoefficients = 0

for i in range (0, len(listScores)):
# calculate the sum of the sum(coefficients*scores)
average = average + listScores [i] * listCoefficients [i]
# calculate the sum of the coefficients
sumCoefficients = sumCoefficients + listCoefficients [i]
average = average/sumCoefficients
return average

# Example
listScores = [16, 14, 12]
listCoefficients = [2, 1, 4]
print ("The average is:", average (listScores, listCoefficients))
# The output is: The average is: 13.428571428571429
Younes Derfoufi
my-courses.net

Leave a Reply