Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Making a weighted assignment, grade book. Don't know where to start..

My goal for this project is to take assignments that have 'weight' to it, get the average, then combine them for the overall grade.

For eg: Homework is 10% | Assignments are 30% | Assessments are 60%

I've created a simple 'All grades are the same weight' code. Which essentially just gets the sum and divides by the len. However, I'm having problems thinking about how to add those 'weight values' to each category. Does anybody have any ideas?

Here is my current 'same weight code' :

import math

#Function that gets the average 
def Average(homework):
    return sum(homework) / len(homework)

homework = [100, 95, 100, 90]
average1 = Average(homework)
print("Average of homework =", round(average1, 2))

classwork = [80, 100, 75, 80]
average2 = Average(classwork)
print("Average of classwork =", round(average2, 2))

assessments = [95, 67, 82]
average3 = Average(assessments)
print("Average of assesments =", round(average3, 2))

totalAverage = average1 + average2 + average3
sameWeightAverage = totalAverage / 3
print("Your total grade is : ", round(sameWeightAverage, 2))

What do u mean by adding to each category?

<noob /> I'm considering 'Homework / Assignments/ Assessments' as categories. And the percent values as 'weights'. So Homework is weighted 10% of the overall grade, Assignments 30%, etc. I just can't figure out how to assign those weights to them.

1 Answer

Steven Parker
Steven Parker
243,266 Points

You can multiply each category average by its "weight", and then add them together.

#                   Homework 10%, Assignments 30%, Assessments 60%
weightedAverage = average1 * 0.1 + average2 * 0.3 + average3 * 0.6