Problem overview
Version 2 of the Report Card project replaces the plain average with a weighted score. Three marks are given: homework = 90, midterm = 82 and final = 88. They count for 20%, 30% and 50% of the final score, and the three weights add up to 100%.
Each weighted part is rounded down to a whole number before the parts are added. For the starter data the parts are 18, 24 and 44, so the result is 86. Store it in a variable named score. The lesson's tests print the label, so the expected output reads Score: 86 and your code prints nothing itself.
Approach: a percentage is a multiplication and a division by 100
Taking 20% of a mark means multiplying it by 20 and dividing by 100. Doing it in that order with whole numbers keeps everything an integer: the multiplication is exact, and // 100 then drops whatever is left over.
The rounding happens per part, not at the end. That is the whole difference between this lesson's formula and a plain percentage of the total, and it can change the answer by a point or two. With three marks of 55, the exact weighted score is 55, but rounding each part separately gives 11 + 16 + 27 = 54.
55 * 20 // 100 = 11
55 * 30 // 100 = 16
55 * 50 // 100 = 27
sum = 54
Since the lesson's rule is "round each part", write three separate divisions and add them.
Step by step
- Homework part. Multiply
homeworkby 20 and divide by 100 with//. For 90 that is 1800 // 100 = 18. - Midterm part. Multiply
midtermby 30 and divide by 100. For 82 that is 2460 // 100 = 24, because the leftover 0.6 is dropped. - Final part. Multiply
finalby 50 and divide by 100. For 88 that is 44. - Add the three parts and store the sum in
score.
The solution in Python
homework = 90
midterm = 82
final = 88
score = homework * 20 // 100 + midterm * 30 // 100 + final * 50 // 100
Python evaluates * and // from left to right, and both bind more tightly than +. So homework * 20 // 100 reads as (homework * 20) // 100, which is what we want, and no parentheses are needed. Multiplying first matters: homework // 100 * 20 would divide 90 by 100 first, get 0, and lose the whole part.
Edge cases and common mistakes
- Dividing before multiplying.
82 // 100 * 30is 0. Multiply by the weight first, then divide by 100. - Using
/.90 * 20 / 100is18.0, a float, and for 91 it is18.2. The result would no longer be a whole number, and the tests expect one. - Rounding once at the end.
(homework * 20 + midterm * 30 + final * 50) // 100is a different formula. With all three marks equal to 55 it gives 55, while the lesson's per-part rounding gives 54. With marks of 99 it gives 99 against 97. Follow the rule that rounds each part. - Weights that don't add up to 100. Here 20 + 30 + 50 = 100. If they didn't, the score would not be on the same scale as the marks.
Key takeaways
- A percentage of a whole number is
value * percent // 100. - Multiply first, divide second, so the division rounds only once per part.
- Where the rounding happens changes the result: here it happens in every part, before adding.
