基于Python的学生信息管理系统设计与实现
时间: 2023-10-09 11:14:11 浏览: 158
基于python的学生信息管理系统设计
学生信息管理系统是一种常见的信息管理工具,可以用来管理学生的个人信息、课程信息、成绩信息等。下面是一份基于Python的学生信息管理系统设计与实现的示例代码:
```
# -*- coding:utf-8 -*-
import os
import pickle
class Student:
def __init__(self, name, age, gender, student_id):
self.name = name
self.age = age
self.gender = gender
self.student_id = student_id
class Course:
def __init__(self, course_name, teacher):
self.course_name = course_name
self.teacher = teacher
class Score:
def __init__(self, student_id, course_name, score):
self.student_id = student_id
self.course_name = course_name
self.score = score
class StudentSystem:
def __init__(self):
self.students = []
self.courses = []
self.scores = []
self.load_data()
def load_data(self):
if os.path.exists('students.pickle'):
with open('students.pickle', 'rb') as f:
self.students = pickle.load(f)
if os.path.exists('courses.pickle'):
with open('courses.pickle', 'rb') as f:
self.courses = pickle.load(f)
if os.path.exists('scores.pickle'):
with open('scores.pickle', 'rb') as f:
self.scores = pickle.load(f)
def save_data(self):
with open('students.pickle', 'wb') as f:
pickle.dump(self.students, f)
with open('courses.pickle', 'wb') as f:
pickle.dump(self.courses, f)
with open('scores.pickle', 'wb') as f:
pickle.dump(self.scores, f)
def add_student(self, name, age, gender, student_id):
student = Student(name, age, gender, student_id)
self.students.append(student)
self.save_data()
def add_course(self, course_name, teacher):
course = Course(course_name, teacher)
self.courses.append(course)
self.save_data()
def add_score(self, student_id, course_name, score):
score = Score(student_id, course_name, score)
self.scores.append(score)
self.save_data()
def get_student(self, student_id):
for student in self.students:
if student.student_id == student_id:
return student
return None
def get_course(self, course_name):
for course in self.courses:
if course.course_name == course_name:
return course
return None
def get_score(self, student_id, course_name):
for score in self.scores:
if score.student_id == student_id and score.course_name == course_name:
return score.score
return None
if __name__ == '__main__':
student_system = StudentSystem()
student_system.add_student('张三', 20, '男', '001')
student_system.add_student('李四', 21, '女', '002')
student_system.add_course('Python', '王老师')
student_system.add_course('Java', '李老师')
student_system.add_score('001', 'Python', 80)
student_system.add_score('002', 'Java', 90)
print(student_system.get_student('001').name)
print(student_system.get_course('Java').teacher)
print(student_system.get_score('002', 'Java'))
```
该代码使用了面向对象的编程思想,定义了三个类分别表示学生、课程和成绩。StudentSystem 类实现了对学生、课程和成绩的增、删、改、查等基本操作,并通过 pickle 模块实现了数据的持久化。在 main 函数中,我们创建了一个 StudentSystem 对象,并添加了一些学生、课程和成绩信息,最后通过 get_student、get_course 和 get_score 方法获取了相应的信息。
阅读全文