I have a list consisting of 4 attributes: subject, test, score, and result. I need to calculate the total score for each subject, by adding up the test scores for each subject. I currently have that. But I need to calculate the total test score of passed tests, and then divide that number by the total test score of all tests.
This is the first part of the code that works correctly:
from collections import defaultdict
d = defaultdict(float)
dc = defaultdict(float)
subject = ['Math', 'Math', 'Math', 'Math', 'Biology', 'Biology', 'Chemistry']
test = ['Test 1','Test 2','Test 3','Test 4','Test 1','Test 2','Test 1']
score = ['1.0', '0.0', '4.0', '0.0', '4.0', '6.0', '2.0']
result = ['fail', 'fail', 'pass', 'fail', 'fail', 'pass', 'pass']
points = [float(x) for x in score]
mylist = list(zip(subject, test, points, result))
for subject, test, points, completion, in mylist:
d[subject] += points
dc[(subject, test)] += points
print(d)
Expected result & actual result is:
{'Math': 5.0, 'Biology': 10.0, 'Chemistry': 2.0}
Now the issue I'm having is I need to add up the total number of points for each subject on only the tests that have been passed. And then divide that number from the total number of all tests (passed and failed) in a subject. So something like, 'if result == "passed" then do 'rest of calculations'.
This is the remaining code:
dc = {f"{subject} {test}" : round(points / d[subject], 2)
if d[subject]!=0 else 'division by zero'
for (subject, test), points in dc.items()}
print(dc)
Expected result:
Math: 4/5, Biology: 6/10, Chemistry: 2/2
Actual result:
'Math Test 1': 0.2, 'Math Test 2': 0.0, 'Math Test 3': 0.8, 'Math Test 4': 0.0, 'Biology Test 1': 0.4, 'Biology Test 2': 0.6, 'Chemistry Test 1': 1.0