用python创建学生信息管理系统要求添加学生信息 删除学生信息 修改学生信息 查询学生信息 统计学生信息,包括学生的姓名、学号、年龄、性别、班级等信息,并注释
时间: 2023-12-21 09:07:26 浏览: 75
好的,下面是一个简单的示例代码,包括添加学生信息、删除学生信息、修改学生信息、查询学生信息和统计学生信息的功能,并添加了注释说明。
```python
class Student:
def __init__(self, name, student_id, age, gender, class_name):
self.name = name
self.student_id = student_id
self.age = age
self.gender = gender
self.class_name = class_name
class StudentManager:
def __init__(self):
self.students = []
# 添加学生信息
def add_student(self, name, student_id, age, gender, class_name):
student = Student(name, student_id, age, gender, class_name)
self.students.append(student)
print("学生信息添加成功!")
# 删除学生信息
def remove_student(self, student_id):
for student in self.students:
if student.student_id == student_id:
self.students.remove(student)
print("学生信息删除成功!")
return
print("学生信息不存在!")
# 修改学生信息
def edit_student(self, student_id, name=None, age=None, gender=None, class_name=None):
for student in self.students:
if student.student_id == student_id:
if name:
student.name = name
if age:
student.age = age
if gender:
student.gender = gender
if class_name:
student.class_name = class_name
print("学生信息修改成功!")
return
print("学生信息不存在!")
# 查询学生信息
def query_student(self, student_id=None, name=None):
if student_id:
for student in self.students:
if student.student_id == student_id:
return student
elif name:
for student in self.students:
if student.name == name:
return student
else:
return self.students
print("学生信息不存在!")
# 统计学生信息
def count_student(self):
print("学生总数为:", len(self.students))
# 创建学生管理器对象
manager = StudentManager()
# 添加学生信息
manager.add_student("张三", "001", 18, "男", "一班")
manager.add_student("李四", "002", 19, "女", "二班")
manager.add_student("王五", "003", 20, "男", "三班")
# 删除学生信息
manager.remove_student("002")
# 修改学生信息
manager.edit_student("001", name="张三丰", age=20)
# 查询学生信息
print(manager.query_student(student_id="001").name)
print(manager.query_student(name="王五").student_id)
print(manager.query_student())
# 统计学生信息
manager.count_student()
```
以上代码仅为示例,具体实现方式可以根据实际需求进行调整。
阅读全文