Home itemized multiplication issue in for loop, python

# itemized multiplication issue in for loop, python

JMatth
1#
JMatth Published in 2018-02-14 01:29:06Z
 I am working on a program to calculate the weighted average of multiple lists of weights and grades. I need to create custom exceptions because not all the data is in the proper format. I am running into two problems, how do I create helpful exceptions and still run all of the lists and two my itemize_mult variable is throwing an error that 'a' is not defined. def weighted_avg(grades, weights): for b in weights: if b < 0: (print("For {}".format(weights), "each weight must be greater than zero.")) raise Exception if b > 100: (print("For {}".format(weights), "each weight cannot be more than 100.")) raise Exception if sum(weights) != 100: (print("For {},".format(weights), "the sum of the weights does not equal 100.")) raise Exception for a in grades: if a in grades != b in weights: (print("For {}{}".format(grades, weights, "the number of grades must match the number of weights."))) raise Exception else: itemized_mult = [a*b for grades, weights in zip(grades, weights)] combined_weight_grade = sum(itemized_mult) output = combined_weight_grade * 100 print(output) print(itemized_mult) grades1 = [88,99,100,70] weights1 = [30, 30, 30, 5] grades2 = [78, 75, 80, 99] weights2 = [110, 10, -20, 0] grades3 = [84, 80, 67, 97] weights3 = [50, 25, 25] grades4 = [100, 80, 90, 75] weights4 = [20, 25, 25, 30] x =(weighted_avg(grades1, weights1)) y =(weighted_avg(grades2, weights2)) z =(weighted_avg(grades3, weights3)) zzz =(weighted_avg(grades4, weights4)) print("List: ", itemized_mult, "Your weighted grade is: ", x) print("List: ", itemized_mult, "Your weighted grade is: ", y) print("List: ", itemized_mult, "Your weighted grade is: ", z) print("List: ", itemized_mult, "Your weighted grade is: ", zzz) 
Brendan Abel
2#
Brendan Abel Reply to 2018-02-14 01:38:11Z
 When you raise an exception, you can pass in an exception message raise Exception('Put your useful message here')  You can create custom exceptions like this class MyException(Exception): def __init__(self, data, *args, **kwargs): super(MyException, self).__init__(*args, **kwargs) self.data = data  Notice how it also accepts some custom data in addition to an exception message. def func(): raise MyException(50, 'This is the exception message')  You can get access to those attributes like this: try: func() except MyException as e: print(e.message) print(e.data) 
jp_data_analysis
3#
 Here is an attempt at cleaning up your code. I have used assert widely for simplicity, but you can equally raise Exception if desired. def weighted_avg(grades, weights): # overall checks assert sum(weights) == 100, "Sum of weights must equal zero: {0}".format(sum(weights)) assert len(grades) == len(weights), "the number of grades must match the number of weights: {0} vs {1}".format(len(grades), len(weights)) # specific checks for b in weights: assert b >= 0, "Each weight must be greater than zero: {0}".format(b) assert b <= 100, "Each weight cannot be more than 100: {0}".format(b) # results itemized_mult = [a*b for a, b in zip(grades, weights)] combined_weight_grade = sum(itemized_mult) output = combined_weight_grade / 100 return itemized_mult, combined_weight_grade, output grades1, weights1 = [88,99,100,70], [30, 30, 35, 5] grades2, weights2 = [78, 75, 80, 99], [90, 10, 0, 0] grades3, weights3 = [84, 80, 67, 97], [50, 25, 25, 0] grades4, weights4 = [100, 80, 90, 75], [20, 25, 25, 30] a = weighted_avg(grades1, weights1) b = weighted_avg(grades2, weights2) c = weighted_avg(grades3, weights3) d = weighted_avg(grades4, weights4) print("List: ", a[0], "Your weighted grade is: ", a[2]) print("List: ", b[0], "Your weighted grade is: ", b[2]) print("List: ", c[0], "Your weighted grade is: ", c[2]) print("List: ", d[0], "Your weighted grade is: ", d[2])