How to Calculate Weighted Grades and Generate Mock Notes in Python

Compute a weighted physics grade from exam and homework scores, then generate a performance-based mock note with percentage and feedback.

Easy Python 3.9+ Aug 9, 2026 A/B testing & experimentation 10 views 0 copies

Python code

33 lines
Python 3.9+
def get_physics_grade(exam_score, homework_score):
    """Calculate final grade from exam and homework scores."""
    exam_weight = 0.7
    homework_weight = 0.3
    return (exam_score * exam_weight) + (homework_score * homework_weight)


def mock_note(correct_score, max_score, student_name):
    """Generate a mock note based on correction logic."""
    percentage = correct_score / max_score * 100
    if percentage >= 90:
        note = "Excellent work"
    elif percentage >= 75:
        note = "Good effort"
    elif percentage >= 50:
        note = "Needs improvement"
    else:
        note = "Must revisit fundamentals"
    return f"{student_name}: {note} ({percentage:.1f}%)"


if __name__ == "__main__":
    # Correcting the mock exam scores
    exam = 82
    homework = 95
    final = get_physics_grade(exam, homework)
    print(f"Adjusted physics grade: {final:.1f}")
    
    # Adding a pelican peeking reference in the mock note
    student = "Tara"
    mock = mock_note(final, 100, student)
    print(mock)
    print("Note: The pelican peeked at the answer key, so we corrected the scores.")

Output

stdout
Adjusted physics grade: 85.9
Tara: Good effort (85.9%)
Note: The pelican peeked at the answer key, so we corrected the scores.

How it works

The get_physics_grade function applies fixed weights (70% exam, 30% homework) to combine two scores into a final grade. The mock_note function converts that score into a percentage and maps it to a qualitative note using chained conditional comparisons. String formatting with :.1f rounds the percentage to one decimal place for clean output. The guard if __name__ == "__main__" lets the script run directly while keeping the functions importable elsewhere.

Common mistakes

  • Dividing by zero if max_score is 0 — add a guard or validate input
  • Using integer division instead of true division for the percentage calculation
  • Forgetting f-string formatting syntax when embedding variables in output

Variations

  1. Use `round(percentage, 1)` instead of f-string formatting for the final value
  2. Return a tuple of note and percentage instead of a formatted string
  3. Add a `max_score` parameter to `get_physics_grade` for modularity

Real-world use cases

  • An instructor generating performance feedback from automatically graded quizzes.
  • An analytics script calculating weighted KPIs and labeling them as pass or fail.
  • A student app scoring assignments and producing motivational messages from results.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from A/B testing & experimentation

Related tutorials and quizzes for this topic.