用python写一个学生信管理系统
时间: 2023-07-18 17:12:27 浏览: 90
用python写的学生管理系统
好的,以下是一个简单的学生信息管理系统的Python代码:
```python
# 定义一个学生类
class Student:
def __init__(self, name, id, grade):
self.name = name
self.id = id
self.grade = grade
# 定义一个学生信息管理系统类
class StudentManagementSystem:
def __init__(self):
self.students = []
# 添加学生信息
def add_student(self, name, id, grade):
student = Student(name, id, grade)
self.students.append(student)
# 删除学生信息
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
# 修改学生信息
def modify_student(self, id, name=None, grade=None):
for student in self.students:
if student.id == id:
if name:
student.name = name
if grade:
student.grade = grade
return True
return False
# 查询学生信息
def query_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
# 显示所有学生信息
def display_students(self):
for student in self.students:
print("Name: {}, ID: {}, Grade: {}".format(student.name, student.id, student.grade))
# 测试代码
if __name__ == '__main__':
sms = StudentManagementSystem()
# 添加学生信息
sms.add_student("Jack", 1001, 90)
sms.add_student("Lucy", 1002, 80)
sms.add_student("Tom", 1003, 70)
# 显示学生信息
sms.display_students()
# 修改学生信息
sms.modify_student(1002, name="Lucy Smith", grade=85)
print("\nAfter modify:")
sms.display_students()
# 查询学生信息
student = sms.query_student(1003)
if student:
print("\nQuery result:")
print("Name: {}, ID: {}, Grade: {}".format(student.name, student.id, student.grade))
# 删除学生信息
sms.remove_student(1001)
print("\nAfter remove:")
sms.display_students()
```
这个学生信息管理系统可以添加、删除、修改、查询和显示所有学生的信息。你可以根据需要进行修改和扩展。
阅读全文