Python学生管理系统完整代码
时间: 2023-08-14 10:09:55 浏览: 355
python学生管理系统代码实现
以下是一个简单的Python学生管理系统的完整代码:
```python
# coding:utf-8
class Student:
def __init__(self, name, age, gender, score):
self.name = name
self.age = age
self.gender = gender
self.score = score
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
def get_student(self, name):
for student in self.students:
if student.name == name:
return student
def get_all_students(self):
return self.students
def get_average_score(self):
total_score = 0
for student in self.students:
total_score += student.score
return total_score / len(self.students)
if __name__ == '__main__':
system = StudentManagementSystem()
student1 = Student('Tom', 18, 'male', 90)
student2 = Student('Jerry', 19, 'female', 85)
student3 = Student('Lily', 20, 'female', 88)
system.add_student(student1)
system.add_student(student2)
system.add_student(student3)
print('All students:')
for student in system.get_all_students():
print(student.name, student.age, student.gender, student.score)
print('Average score:', system.get_average_score())
system.remove_student('Jerry')
print('All students after removing Jerry:')
for student in system.get_all_students():
print(student.name, student.age, student.gender, student.score)
```
上述代码实现了以下功能:
1. 定义了一个学生类,其中包含学生的姓名、年龄、性别和成绩等信息。
2. 定义了一个学生管理系统类,其中包含了添加学生、删除学生、获取单个学生、获取所有学生和计算平均成绩等方法。
3. 在程序的主函数中,创建了一个学生管理系统的实例,并添加了三个学生的信息。
4. 输出了所有学生的信息和平均成绩。
5. 删除了一个学生的信息,并重新输出了所有学生的信息。
阅读全文