Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Advanced Grade Calculator

Calculate a weighted grade average in Python. Students multiply marks by weights, divide by total weight, and apply descending grade cutoffs to the actual mean.

AdvancedPhase 6 - Category 5: Real-World Logic ProblemsExample 2 of 15
advanced-grade-calculator.py
1# Take number of subjects
2n = int(input("Enter number of subjects: "))
3
4# Take marks and weights
5total_weighted = 0
6total_weight = 0
7
8for i in range(n):
9 marks = float(input(f"Subject {i+1} marks: "))
10 weight = float(input(f"Subject {i+1} weight: "))
11 total_weighted += marks * weight
12 total_weight += weight
13
14# Calculate weighted average
15average = total_weighted / total_weight
16
17# Assign grade
18if average >= 90:
19 grade = "A+"
20elif average >= 80:
21 grade = "A"
22elif average >= 70:
23 grade = "B"
24elif average >= 60:
25 grade = "C"
26else:
27 grade = "F"
28
29print(f"Weighted average: {average:.2f}")
30print(f"Grade: {grade}")

Output

Enter number of subjects: 3
Subject 1 marks: 85
Subject 1 weight: 2
Subject 2 marks: 90
Subject 2 weight: 3
Subject 3 marks: 75
Subject 3 weight: 1
Weighted average: 85.83
Grade: A

In this one

  • Weight marks proportionally
  • Normalize by total weight
  • Grade the unrounded mean

How it works

  1. 1

    Accumulate marks * weight and total_weight.

  2. 2

    Divide weighted total by total weight.

  3. 3

    Apply descending cutoffs, then print formatted average and grade.

Edge cases

What if every weight is zero?

What if every weight is zero?

The average calculation raises ZeroDivisionError.

Are grades based on the rounded display?

Are grades based on the rounded display?

No. Conditions use the stored average.

What's going on

Each subject adds marks * weight to total_weighted and weight to total_weight. Dividing those totals accounts for subjects having different influence.

The grade chain begins at ninety and moves downward. The displayed mean uses two decimals, but grading uses the unrounded value. The sample weighted sum is 515 and total weight six. A zero total weight causes division by zero; valid input should use positive weights.

What if every weight is zero? The average calculation raises ZeroDivisionError.

Are grades based on the rounded display? No. Conditions use the stored average.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why divide by total weight rather than subject count?

Pick an answer to check it.

Question 2

What if every weight is zero?

Pick an answer to check it.

Questions people ask

What if every weight is zero?

The average calculation raises ZeroDivisionError.

Are grades based on the rounded display?

No. Conditions use the stored average.

Why divide by total weight rather than subject count?

Weights determine relative contributions.