-2

I'd like to find the average of the student's test grades and hw, then run a function that gives them a final score assuming HW is 20% and Test is 80%

student_tests = {
    'Stan_test': [64, 90, 78, 80],
    'Richard_tests': [99, 87, 92, 90],
    'Nicole_tests': [6, 66, 6, 66],
    'David_tests': [78, 91, 92, 96], }

student_hw = {
    'Sten_hw': [100, 90, 85, 99, 46],
    'Richard_hw': [96, 66, 94, 77, 88],
    'Nicole_hw': [100, 100, 100, 100, 100],
    'David_hw': [68, 71, 74, 77, 80] }

results_tests = {}
for k, v in student_tests.items():
    if type(v) in [float, int]:
        results_tests[k] = v
    else:
        results_tests[k] = sum(v) / len(v)
print(results_tests)
results_hw = {}
for k, v in student_hw.items():
    if type(v) in [float, int]:
        results_hw[k] = v
    else:
        results_hw[k] = sum(v) / len(v)
print(results_hw)

def calculate_average():
    for i in results_tests.values():
        i = i * 80 / 100
    for a in results_hw.values():
        a = a * 20 / 100
    b = i + a
    print(b)

calculate_average()
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
cool mouse
  • 35
  • 5
  • 1
    What is your question? How is the code not behaving they way you want it to? – RoadRunner May 07 '20 at 12:54
  • I attempted to fix your indentation, it was jacked-up. – Mr. Polywhirl May 07 '20 at 12:57
  • Can we assume that the order in which students' scores are stored for the homework and tests are identical (assuming you're running Python 3.7 or higher, where dictionary orders are preserved)? Or do we perhaps have a list of student names? – Jake Tae May 07 '20 at 12:58

1 Answers1

0

I'm going to assume that the score dictionaries are ordered, i.e. the nth key in student_hw and the nth key in student_tests correspond to the same student. (For Python 3.7 and above, dictionaries are order-preserved. Refer here for a detailed discussion on this topic.)

Let's approach the problem in a modular fashion. We can begin by first defining a function that returns the average scores given a dictionary, whether it be for a test or a homework assignment.

 def get_average(score_dict):
     res = []
     for score in score_dict.values():
         res.append(sum(score) / len(score))
     return res

If you prefer one-liners and short coding,

def short_get_average(score_dict):
    return [sum(score) / len(score) for score in score_dict.values()]

Using the function we defined above, we can calculate the total score for each student, using the 80-20 grade weighting formula.

def total_score(hw_scores, test_scores):
    return [hw * 0.2 + test * 0.8 for hw, test in zip(hw_scores, test_scores)] 

Let's run through each step:

hw_scores = get_average(student_hw)
test_scores = get_average(student_tests)
final_scores = total_score(hw_scores, test_scores)

The result:

>>> final_scores
[79.2, 90.44000000000001, 48.8, 86.2]
Jake Tae
  • 1,681
  • 1
  • 8
  • 11