使用python写一个中学生管理系统
时间: 2023-05-21 07:06:55 浏览: 192
以下是基于 Python 实现的中学生管理系统的代码:
```python
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class ManagementSystem:
def __init__(self):
self.students = []
def add_student(self, name, age, gender):
student = Student(name, age, gender)
self.students.append(student)
def remove_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
def get_students(self):
return [student.name for student in self.students]
def get_student_info(self, name):
for student in self.students:
if student.name == name:
return f"Name: {student.name}, Age: {student.age}, Gender: {student.gender}"
return f"{name} is not in the system"
if __name__ == "__main__":
management_system = ManagementSystem()
management_system.add_student("Tom", 15, "Male")
management_system.add_student("Lucy", 16, "Female")
print(management_system.get_students())
print(management_system.get_student_info("Tom"))
management_system.remove_student("Tom")
print(management_system.get_students())
```
这是一个简单的中学生管理系统,其中包含一个 `Student` 类和一个 `ManagementSystem` 类。`Student` 类存储学生的姓名、年龄和性别,`ManagementSystem` 类用于管理所有学生的信息,包括添加、删除和检索学生信息。上述代码中,我们演示了如何创建一个 `ManagementSystem` 对象、添加学生、打印所有学生的姓名、按名字检索学生信息以及移除一个学生。
阅读全文