Categories We Write About

Build a student grade tracker

Building a student grade tracker involves creating a system that allows you to input, store, calculate, and analyze students’ grades efficiently. Below is a comprehensive guide to building a basic but fully functional student grade tracker using Python. This tracker will allow you to:

  • Add students and their grades

  • Calculate average grades

  • Display grades and averages

  • Save and load the data for persistence


Student Grade Tracker in Python

python
class StudentGradeTracker: def __init__(self): self.students = {} def add_student(self, student_name): if student_name in self.students: print(f"Student '{student_name}' already exists.") else: self.students[student_name] = [] print(f"Student '{student_name}' added.") def add_grade(self, student_name, grade): if student_name not in self.students: print(f"Student '{student_name}' not found. Please add the student first.") else: if 0 <= grade <= 100: self.students[student_name].append(grade) print(f"Added grade {grade} for student '{student_name}'.") else: print("Grade must be between 0 and 100.") def get_average(self, student_name): if student_name not in self.students: print(f"Student '{student_name}' not found.") return None grades = self.students[student_name] if not grades: print(f"No grades recorded for '{student_name}'.") return None return sum(grades) / len(grades) def print_student_report(self, student_name): if student_name not in self.students: print(f"Student '{student_name}' not found.") return grades = self.students[student_name] average = self.get_average(student_name) print(f"Report for {student_name}:") print(f"Grades: {grades if grades else 'No grades recorded.'}") print(f"Average: {average:.2f}" if average is not None else "Average: N/A") def print_all_students(self): if not self.students: print("No students added yet.") return for student_name in self.students: self.print_student_report(student_name) print("-" * 30) def save_to_file(self, filename): import json with open(filename, 'w') as f: json.dump(self.students, f) print(f"Data saved to {filename}.") def load_from_file(self, filename): import json try: with open(filename, 'r') as f: self.students = json.load(f) print(f"Data loaded from {filename}.") except FileNotFoundError: print(f"No such file: {filename}") # Example usage: tracker = StudentGradeTracker() # Adding students tracker.add_student("Alice") tracker.add_student("Bob") # Adding grades tracker.add_grade("Alice", 85) tracker.add_grade("Alice", 90) tracker.add_grade("Bob", 78) # Printing reports tracker.print_all_students() # Save data tracker.save_to_file("grades.json") # Load data tracker.load_from_file("grades.json")

Explanation

  • add_student: Adds a new student to the tracker.

  • add_grade: Adds a grade to a specific student.

  • get_average: Calculates the average grade for a student.

  • print_student_report: Prints all grades and average for a specific student.

  • print_all_students: Prints reports for all students.

  • save_to_file / load_from_file: Saves and loads student data from a JSON file for persistence.

This simple tracker can be expanded with features like weighted grades, grade categories, or user interfaces (CLI, web, or GUI).

Would you like me to build a web or spreadsheet-based grade tracker as well?

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About