Start learning free

LESSON 198 OF 287 · PROJECTS: EVERYDAY PROGRAMS

Report Card V2 in Python: Weighted Score with Integer Division

Practice Read the full explanation

Combine variables and arithmetic to compute a student's weighted final score.

Weights: Homework 20%, Midterm 30%, Final 50%. Each weighted part is rounded down to a whole number before the parts are added.

You start with:

homework = 90
midterm = 82
final = 88

Build:

Score: 86

Rules:

  • Compute score = homework * 20 // 100 + midterm * 30 // 100 + final * 50 // 100.
  • Each part is rounded down by //.
  • For the starting data: 18 + 24 + 44 = 86.
  • Store the result in a variable named score. The checker prints "Score: " followed by its value for you, so do not print anything yourself.

Use // after each multiplication: homework * 20 // 100 + midterm * 30 // 100 + final * 50 // 100.

homework = 90
midterm = 82
final = 88

score = homework * 20 // 100 + midterm * 30 // 100 + final * 50 // 100

STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Report Card V2 in Python, explained

    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

    1. Homework part. Multiply homework by 20 and divide by 100 with //. For 90 that is 1800 // 100 = 18.
    2. Midterm part. Multiply midterm by 30 and divide by 100. For 82 that is 2460 // 100 = 24, because the leftover 0.6 is dropped.
    3. Final part. Multiply final by 50 and divide by 100. For 88 that is 44.
    4. 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 * 30 is 0. Multiply by the weight first, then divide by 100.
    • Using /. 90 * 20 / 100 is 18.0, a float, and for 91 it is 18.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) // 100 is 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.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free