python管理系统代码

以下是一个简单的Python管理系统代码示例:

class Student:
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade

class ManagementSystem:
    def __init__(self):
        self.students = []

    def add_student(self, student):
        self.students.append(student)

    def remove_student(self, student):
        self.students.remove(student)

    def search_student(self, name):
        for student in self.students:
            if student.name == name:
                return student
        return None

    def display_students(self):
        for student in self.students:
            print(f"Name: {student.name}, Age: {student.age}, Grade: {student.grade}")

# 创建管理系统对象
system = ManagementSystem()

# 添加学生
student1 = Student("Alice", 18, "A")
student2 = Student("Bob", 17, "B")
system.add_student(student1)
system.add_student(student2)

# 显示学生列表
system.display_students()

# 搜索学生
search_name = input("请输入要搜索的学生姓名:")
student = system.search_student(search_name)
if student:
    print(f"找到学生:Name: {student.name}, Age: {student.age}, Grade: {student.grade}")
else:
    print("未找到该学生")

# 删除学生
remove_name = input("请输入要删除的学生姓名:")
student = system.search_student(remove_name)
if student:
    system.remove_student(student)
    print(f"已删除学生:Name: {student.name}, Age: {student.age}, Grade: {student.grade}")
else:
    print("未找到该学生")

这个代码示例实现了一个简单的学生管理系统,可以添加学生、删除学生、搜索学生和显示学生列表。

当然,还可以进一步完善这个学生管理系统。以下是一些可能的扩展功能:

修改学生信息:可以添加一个modify_student方法,允许用户修改学生的姓名、年龄和成绩。

def modify_student(self, student, name, age, grade):
    student.name = name
    student.age = age
    student.grade = grade

按照年龄排序:可以添加一个sort_students_by_age方法,按照学生的年龄进行排序,并显示排序后的学生列表。

def sort_students_by_age(self):
    sorted_students = sorted(self.students, key=lambda student: student.age)
    for student in sorted_students:
        print(f"Name: {student.name}, Age: {student.age}, Grade: {student.grade}")

统计学生人数:可以添加一个count_students方法,用于统计学生的总人数。

def count_students(self):
    return len(self.students)

保存和加载数据:可以添加一个save_data方法,将学生数据保存到文件中,以便下次运行时加载数据。

import pickle

def save_data(self, filename):
    with open(filename, "wb") as file:
        pickle.dump(self.students, file)

def load_data(self, filename):
    with open(filename, "rb") as file:
        self.students = pickle.load(file)

这些是一些可能的扩展功能,你可以根据实际需求进行修改和扩展。希望对你有帮助!