用python做一个学生管理系统
时间: 2023-12-19 07:05:27 浏览: 26
好的,下面是一个简单的学生管理系统的Python代码示例:
```python
class Student:
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
self.students.remove(student)
def search_student_by_name(self, name):
for student in self.students:
if student.name == name:
return student
return None
def search_student_by_grade(self, grade):
result = []
for student in self.students:
if student.grade == grade:
result.append(student)
return result
def display_all_students(self):
for student in self.students:
print(f"Name: {student.name}, Age: {student.age}, Gender: {student.gender}, Grade: {student.grade}")
# Create a student management system instance
sms = StudentManagementSystem()
# Add some students
sms.add_student(Student("Alice", 16, "Female", 10))
sms.add_student(Student("Bob", 17, "Male", 11))
sms.add_student(Student("Charlie", 15, "Male", 9))
# Display all students
sms.display_all_students()
# Search for a student by name
student = sms.search_student_by_name("Bob")
if student:
print(f"Found student: Name: {student.name}, Age: {student.age}, Gender: {student.gender}, Grade: {student.grade}")
else:
print("Student not found")
# Search for students by grade
students = sms.search_student_by_grade(10)
if students:
print("Found students:")
for student in students:
print(f"Name: {student.name}, Age: {student.age}, Gender: {student.gender}, Grade: {student.grade}")
else:
print("No students found")
```
这个代码示例定义了两个类:`Student` 和 `StudentManagementSystem`。`Student` 类用于表示一个学生,包含姓名、年龄、性别和年级等信息;`StudentManagementSystem` 类用于管理学生信息,包含添加、删除、搜索和显示学生等方法。
在代码示例中,我们创建了一个学生管理系统实例 `sms`,并添加了三个学生。然后,我们展示了所有学生信息,并通过姓名和年级搜索了学生。你可以根据自己的需求修改代码,实现更复杂的学生管理系统。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)