用python写一个中小学信息管理的代码
时间: 2025-01-04 15:39:52 浏览: 2
编写一个简单的中小学信息管理系统(School Management System),我们可以用Python的字典列表、类等基本数据结构来构建。这个系统可以包含学生、教师和课程的基本信息,如姓名、ID、科目、年级等。下面是一个基础框架的例子:
```python
class Student:
def __init__(self, name, id_number, grade):
self.name = name
self.id_number = id_number
self.grade = grade
class Teacher:
def __init__(self, name, id_number):
self.name = name
self.id_number = id_number
class Course:
def __init__(self, name, teacher):
self.name = name
self.teacher = teacher
# 创建学校类并存储信息
class School:
def __init__(self):
self.students = []
self.teachers = []
self.courses = []
# 添加学生、教师和课程的方法
def add_student(self, student_info):
new_student = Student(*student_info)
self.students.append(new_student)
def add_teacher(self, teacher_info):
new_teacher = Teacher(*teacher_info)
self.teachers.append(new_teacher)
def add_course(self, course_info):
new_course = Course(*course_info)
self.courses.append(new_course)
# 显示所有信息的方法
def display_info(self):
print("Students:")
for student in self.students:
print(f"Name: {student.name}, ID: {student.id_number}, Grade: {student.grade}")
print("\nTeachers:")
for teacher in self.teachers:
print(f"Name: {teacher.name}, ID: {teacher.id_number}")
print("\nCourses:")
for course in self.courses:
print(f"Course Name: {course.name}, Teacher: {course.teacher.name}")
# 使用示例
school = School()
school.add_student(["Alice", "001", "7th"])
school.add_teacher(["Mr. Zhang", "002"])
school.add_course(["Math", school.teachers[0]])
school.display_info()
阅读全文