Just Weight and See


#1

Good Morning guys,

I’m just wondering what are the “weights” and whether those numbers were just out of preference?


#2

Context? A link to the exercise, perhaps? Some of your code? :slight_smile:


#3

Sure here you go. Again where do the “weights” come in, couldn’t we work out the average of the student keys without them?

lloyd = {
  "name": "Lloyd",
  "homework": [90.0, 97.0, 75.0, 92.0],
  "quizzes": [88.0, 40.0, 94.0],
  "tests": [75.0, 90.0]
}
alice = {
  "name": "Alice",
  "homework": [100.0, 92.0, 98.0, 100.0],
  "quizzes": [82.0, 83.0, 91.0],
  "tests": [89.0, 97.0]
}
tyler = {
  "name": "Tyler",
  "homework": [0.0, 87.0, 75.0, 22.0],
  "quizzes": [0.0, 75.0, 78.0],
  "tests": [100.0, 100.0]
}

# we create a function that divides the sum of the numbers by the length(len) of the numbers.
def average(numbers):
  total = sum(numbers)
  total = float(total)
  total = total / len(numbers)
  return total

def get_average(student):
  homework = average(student["homework"])
  quizzes = average(student["quizzes"])
  tests = average(student["tests"])
  return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

def get_letter_grade(score):
  if score >= 90:
    return "A"
  elif score >= 80:
    return "B"
  elif score >= 70:
    return "C"
  elif score >= 60:
    return "D"
  else:
    return "F"
  
def get_class_average(class_list):
  results = []
  for student in class_list:
    results.append(get_average(student))
  return average(results)

students = [alice, lloyd, tyler]
print get_letter_grade(get_class_average(students))
print get_class_average(students)


#4

From a pure math perspective…

The weighting is done to make it so some grades are worth more than others. In this case, 10% of the grade is the average for homework, 30% is the average for quizzes, and 60% is the average for tests. This essentially ranks each type of average in order of “importance”. What counts for more - 30 days worth of homework, or a test?

The programming aspect of this is just implemented with a math expression. For clarity, I’d even go so far as to use parentheses:

return (0.1*homework) + (0.3*quizzes) + (0.6*tests)

This way, you can clearly see how each grade is affected.


#5

I think I’ve got the jist of it. Thanks :slight_smile:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.