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.
1# Take number of subjects2n = int(input("Enter number of subjects: "))34# Take marks and weights5total_weighted = 06total_weight = 078for 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 * weight12 total_weight += weight1314# Calculate weighted average15average = total_weighted / total_weight1617# Assign grade18if 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"2829print(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
Accumulate marks * weight and total_weight.
- 2
Divide weighted total by total weight.
- 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.
๐Related Content
- ๐
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- ๐
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- ๐ป
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum