使用面向对象的思想实现学生管理系统 1、增加学生 姓名、性别、年龄、住址(省份市区--如河南洛阳) 都重复就不添加 2、根据序号删除学生 3、根据序号修改学生 4、查询所有的学生信息 5、查询所有的男生 6、查询河南的学生 7、找出未成年学生
时间: 2024-02-01 19:02:59 浏览: 52
好的,我可以给你提供一个示例代码,帮助你实现学生管理系统。以下是代码:
```python
class Student:
def __init__(self, name, gender, age, address):
self.name = name
self.gender = gender
self.age = age
self.address = address
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, name, gender, age, address):
for student in self.students:
if student.name == name and student.gender == gender and student.age == age and student.address == address:
print("该学生已存在!")
return
new_student = Student(name, gender, age, address)
self.students.append(new_student)
print("添加成功!")
def remove_student(self, student_id):
if student_id < 0 or student_id >= len(self.students):
print("该学生不存在!")
return
self.students.pop(student_id)
print("删除成功!")
def update_student(self, student_id, name, gender, age, address):
if student_id < 0 or student_id >= len(self.students):
print("该学生不存在!")
return
for student in self.students:
if student.name == name and student.gender == gender and student.age == age and student.address == address:
print("该学生已存在!")
return
self.students[student_id].name = name
self.students[student_id].gender = gender
self.students[student_id].age = age
self.students[student_id].address = address
print("修改成功!")
def show_all_students(self):
if not self.students:
print("没有学生信息!")
return
for i, student in enumerate(self.students):
print(f"序号:{i}, 姓名:{student.name}, 性别:{student.gender}, 年龄:{student.age}, 地址:{student.address}")
def show_all_male_students(self):
male_students = []
for student in self.students:
if student.gender == "男":
male_students.append(student)
if not male_students:
print("没有男生!")
return
for i, student in enumerate(male_students):
print(f"序号:{i}, 姓名:{student.name}, 性别:{student.gender}, 年龄:{student.age}, 地址:{student.address}")
def show_all_henan_students(self):
henan_students = []
for student in self.students:
if "河南" in student.address:
henan_students.append(student)
if not henan_students:
print("没有河南学生!")
return
for i, student in enumerate(henan_students):
print(f"序号:{i}, 姓名:{student.name}, 性别:{student.gender}, 年龄:{student.age}, 地址:{student.address}")
def show_all_underage_students(self):
underage_students = []
for student in self.students:
if student.age < 18:
underage_students.append(student)
if not underage_students:
print("没有未成年学生!")
return
for i, student in enumerate(underage_students):
print(f"序号:{i}, 姓名:{student.name}, 性别:{student.gender}, 年龄:{student.age}, 地址:{student.address}")
```
以上是一个简单的学生管理系统示例代码,你可以在此基础上进行改进和扩展。
阅读全文