Comprehensive Python Project: Student Grade Management System (Command Line Version)

After learning the basic syntax, functions, file operations, and data structures of Python, the most important step is to “combine the scattered skills into practice”. A command line version of a student grade management system is very suitable as a small project at this stage, as it does not require any GUI framework but covers the following key knowledge points:

  • • File reading and writing
  • • List and dictionary operations
  • • Input and output
  • • Control flow and exception handling
  • • Modularization (optional)
  • • Program structure design

Although these small projects are simple, they are very close to real business logic and represent the first step from “syntax” to “small applications”.

1. Project Function Design

First, we need to clearly plan the project functions, which is more important than writing code:

Core Requirements

  1. 1. Add student grades
  2. 2. View all student grades
  3. 3. Query a specific student
  4. 4. Modify student grades
  5. 5. Delete student information
  6. 6. Save to file / Read from file
  7. 7. Exit the program

Data Structure Design

We represent a student using a dictionary:

{
    "id": "2025001",
    "name": "Zhang San",
    "score": 88
}

All student data is stored in a list:

students = []

This structure is clear, extensible, and convenient for serialization and storage.

2. Main Menu Design

The most important part of a command line program is the menu loop:

def show_menu():
    print("\n===== Student Grade Management System =====")
    print("1. Add student grades")
    print("2. View all students")
    print("3. Query student")
    print("4. Modify student grades")
    print("5. Delete student information")
    print("6. Save data")
    print("7. Load data")
    print("0. Exit system")

It seems simple, but this is the “entry point” for all subsequent operations.

3. Function Implementation

Below is the complete core logic, with a clear and understandable code structure.

① Add Student

def add_student(students):
    sid = input("Please enter student ID:")
    name = input("Please enter name:")
    score = int(input("Please enter score:"))
    students.append({"id": sid, "name": name, "score": score})
    print("Added successfully!")

② View All Students

def show_all(students):
    if not students:
        print("Currently, there are no student data")
        return
    print("\n--- All Student Grades ---")
    for stu in students:
        print(f"Student ID: {stu['id']}, Name: {stu['name']}, Score: {stu['score']}")

③ Query Student by ID

def find_student(students):
    sid = input("Please enter the student ID to query:")
    for stu in students:
        if stu["id"] == sid:
            print(f"Found → Name: {stu['name']}, Score: {stu['score']}")
            return stu
    print("Student not found")
    return None

④ Update Score

def update_student(students):
    stu = find_student(students)
    if stu:
        new_score = int(input("Please enter new score:"))
        stu["score"] = new_score
        print("Updated successfully!")

⑤ Delete Student

def delete_student(students):
    stu = find_student(students)
    if stu:
        students.remove(stu)
        print("Deleted successfully!")

4. Data Persistence (File Save & Load)

We use a simple JSON format because it is clear in structure and easy to read and write.

import json

def save_data(students, filename="students.json"):
    with open(filename, "w", encoding="utf-8") as f:
        json.dump(students, f, ensure_ascii=False, indent=2)
    print("Data has been saved!")

def load_data(filename="students.json"):
    try:
        with open(filename, "r", encoding="utf-8") as f:
            data = json.load(f)
        print("Data loaded successfully!")
        return data
    except FileNotFoundError:
        print("File not found, using empty data.")
        return []

5. Main Program (Connecting All Functions)

def main():
    students = load_data()

    while True:
        show_menu()
        choice = input("Please select an operation:")

        if choice == "1":
            add_student(students)
        elif choice == "2":
            show_all(students)
        elif choice == "3":
            find_student(students)
        elif choice == "4":
            update_student(students)
        elif choice == "5":
            delete_student(students)
        elif choice == "6":
            save_data(students)
        elif choice == "7":
            students = load_data()
        elif choice == "0":
            print("Exiting the system, welcome to use next time!")
            break
        else:
            print("Invalid input, please select again!")

Run:

python student_system.py

Your first complete small system is ready to run.

6. Project Expansion Directions

If you want to continue upgrading this project, here are some good expansion directions:

✔ Add Grade Sorting Functionality

Sort by score, sort by name, etc.

✔ Add Grade Statistics

Highest score, lowest score, average score.

✔ Use SQLite/MySQL for Persistence

Upgrade from file to a real database.

✔ Use Classes for Encapsulation

Encapsulate students and the system into classes for more professional code.

✔ Use Tkinter / PyQt for GUI

Instantly turn it into a desktop application.

✔ Use Flask / FastAPI to Provide REST API

Make it a real “student management backend”.

Leave a Comment