Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

def weighted_avg(list_of_avg,list_of_weights): numerator = 0 if len(list_of_avg)!=len(list_of_weights): print(Discrepancy in number of weights and averages) elif len(list_of_avg)==len(list_of_weights): for i in range(len(list_of_avg)): numerator = numerator + list_of_avg[i]*list_of_weights[i]

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

def weighted_avg(list_of_avg,list_of_weights): numerator = 0 if len(list_of_avg)!=len(list_of_weights): print("Discrepancy in number of weights and averages") elif len(list_of_avg)==len(list_of_weights): for i in range(len(list_of_avg)): numerator = numerator + list_of_avg[i]*list_of_weights[i] weighted_Avg = numerator/sum(list_of_weights) return weighted_Avg elif len(list_of_avg)==0: return None

Write a function, weighted_avg(...), that computes the weighted average of a list of averages. The function accepts two parameters, list_of avgs and list_of weights. You can assume that the values in the list are either integers or floats. Each value in list_of_avgs has a corresponding weight in list_of_weights. The function should return a single weighted average. If the number of weights does not match the number of averages return "Discrepancy in number of weights and averages". Ex. list-of-avg [98, 60, 40] list-of-weights [0.5, 0.25, 0.25] print( weighted_avg( list_of_avg, list_of_weights) ) the output should be 74 which is (98 0.5+60*0.25+40*0.25)/ (0.5+0.25+0.25)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions