用python设计并实现一个学生信息注册系统
时间: 2023-12-31 14:04:42 浏览: 36
基于python实现学生信息管理系统
5星 · 资源好评率100%
好的,我可以为您提供一个简单的学生信息注册系统的Python代码。该系统可以实现学生信息的添加、删除、修改和查询。代码如下:
```python
# 定义一个学生类
class Student:
def __init__(self, name, age, gender, id):
self.name = name
self.age = age
self.gender = gender
self.id = id
# 定义一个学生管理系统类
class StudentManager:
def __init__(self):
self.students = []
# 添加学生
def add_student(self, student):
self.students.append(student)
# 删除学生
def del_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, age=None, gender=None):
for student in self.students:
if student.id == id:
if name:
student.name = name
if age:
student.age = age
if gender:
student.gender = gender
return True
return False
# 查询学生信息
def query_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
# 测试
if __name__ == '__main__':
student_manager = StudentManager()
student1 = Student('张三', 20, '男', '001')
student2 = Student('李四', 21, '女', '002')
student3 = Student('王五', 22, '男', '003')
student_manager.add_student(student1)
student_manager.add_student(student2)
student_manager.add_student(student3)
print([stu.name for stu in student_manager.students])
student_manager.del_student('002')
print([stu.name for stu in student_manager.students])
student_manager.modify_student('001', name='赵六', age=23)
print([stu.name for stu in student_manager.students])
student = student_manager.query_student('003')
print(student.name, student.age, student.gender, student.id)
```
您可以根据自己的需要对代码进行修改和扩展。
阅读全文