用python编写学生管理系统
时间: 2023-11-23 14:56:03 浏览: 64
学生信息管理系统是一个非常实用的工具,可以帮助教师和管理员更有效地管理学生信息。Python是一种非常适合开发此类系统的语言,因为它具有简单易学、易维护等特点。下面是一个简单的学生信息管理系统的示例:
```python
# 定义一个学生类
class Student:
def __init__(self, name, age, gender, id):
self.name = name
self.age = age
self.gender = gender
self.id = id
# 定义一个学生信息管理系统类
class StudentManagementSystem:
def __init__(self):
self.students = []
# 添加学生信息
def add_student(self, student):
self.students.append(student)
# 根据学生ID查找学生信息
def find_student_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
# 根据学生ID删除学生信息
def delete_student_by_id(self, id):
student = self.find_student_by_id(id)
if student:
self.students.remove(student)
# 修改学生信息
def modify_student(self, id, name, age, gender):
student = self.find_student_by_id(id)
if student:
student.name = name
student.age = age
student.gender = gender
# 显示所有学生信息
def show_all_students(self):
for student in self.students:
print("Name: {}, Age: {}, Gender: {}, ID: {}".format(student.name, student.age, student.gender, student.id))
# 创建一个学生信息管理系统对象
sms = StudentManagementSystem()
# 添加学生信息
s1 = Student("Tom", 18, "Male", "001")
s2 = Student("Lucy", 19, "Female", "002")
sms.add_student(s1)
sms.add_student(s2)
# 查找学生信息
s = sms.find_student_by_id("001")
if s:
print("Name: {}, Age: {}, Gender: {}, ID: {}".format(s.name, s.age, s.gender, s.id))
else:
print("Student not found")
# 修改学生信息
sms.modify_student("001", "Jerry", 20, "Male")
# 删除学生信息
sms.delete_student_by_id("002")
# 显示所有学生信息
sms.show_all_students()
```
阅读全文