python学生管理系统代码案例
时间: 2024-06-23 21:01:35 浏览: 118
学生管理系统实例
Python 学生管理系统是一个常见的教育领域项目,它通常包括用户管理、课程管理、成绩管理和查询等功能。下面是一个简化的 Python 示例,使用了类和文件操作来实现基本的学生信息和成绩管理:
```python
class Student:
def __init__(self, id, name, courses={}):
self.id = id
self.name = name
self.courses = courses
def enroll_course(self, course_id, grade):
if course_id not in self.courses:
self.courses[course_id] = grade
else:
print("学生已注册该课程")
def view_grades(self):
for course_id, grade in self.courses.items():
print(f"课程 {course_id}: {grade}")
# 读取学生信息
def load_students(filename):
students = []
with open(filename, 'r') as file:
lines = file.readlines()
for line in lines:
data = line.strip().split(',')
id, name = data[:2]
student = Student(id, name)
students.append(student)
return students
# 存储学生信息
def save_students(students, filename):
with open(filename, 'w') as file:
for student in students:
file.write(f"{student.id},{student.name}\n")
# 示例用法
students = load_students('students.txt')
student = students # 假设已加载一个学生
student.enroll_course(1, 90) # 注册课程并设置成绩
student.view_grades()
# ... 这里可以添加更多功能,如添加新学生、删除课程等
```
阅读全文