来一份学生管理系统代码
时间: 2023-08-04 14:02:48 浏览: 48
以下是一个简单的学生管理系统代码,包括添加学生信息、查询学生信息、修改学生信息和删除学生信息等功能:
```python
class Student:
def __init__(self, name, age, gender, id):
self.name = name
self.age = age
self.gender = gender
self.id = id
class StudentSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def find_student(self, id):
for student in self.students:
if student.id == id:
return student
def update_student(self, id, name, age, gender):
student = self.find_student(id)
if student:
student.name = name
student.age = age
student.gender = gender
def remove_student(self, id):
student = self.find_student(id)
if student:
self.students.remove(student)
def show_students(self):
for student in self.students:
print("Name:", student.name)
print("Age:", student.age)
print("Gender:", student.gender)
print("ID:", student.id)
print("-----")
if __name__ == "__main__":
system = StudentSystem()
system.add_student(Student("Tom", 18, "Male", "001"))
system.add_student(Student("Jerry", 19, "Female", "002"))
system.show_students()
system.update_student("001", "Tim", 20, "Male")
system.show_students()
system.remove_student("002")
system.show_students()
```
这个代码定义了一个 `Student` 类来表示学生信息,以及一个 `StudentSystem` 类来实现学生管理系统的各种功能。在 `if __name__ == "__main__":` 语句块中,我们创建了一个 `StudentSystem` 对象,并添加了两个学生信息。然后我们展示了所有学生信息,修改了一个学生信息,删除了另一个学生信息,最后再次展示了所有学生信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)