Answered step by step
Verified Expert Solution
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]
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started