如何用pathon实现学绩管理
时间: 2023-05-19 07:07:33 浏览: 74
可以使用 Python 的面向对象编程思想,创建一个名为 Student 的类,包含学生的姓名、学号、各科成绩等属性,以及计算总分和平均分的方法。然后再创建一个名为 Grade 的类,包含多个 Student 对象,以及添加、删除、查询学生信息等方法。最后在主函数中实例化 Grade 类,调用其方法实现学绩管理。具体实现代码可以参考以下示例:
```python
class Student:
def __init__(self, name, id, math, english, chinese):
self.name = name
self.id = id
self.math = math
self.english = english
self.chinese = chinese
def get_total_score(self):
return self.math + self.english + self.chinese
def get_average_score(self):
return self.get_total_score() / 3
class Grade:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
break
def search_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
if __name__ == '__main__':
grade = Grade()
student1 = Student('张三', '001', 90, 80, 70)
student2 = Student('李四', '002', 85, 75, 65)
student3 = Student('王五', '003', 95, 85, 75)
grade.add_student(student1)
grade.add_student(student2)
grade.add_student(student3)
grade.remove_student('002')
student = grade.search_student('001')
if student:
print('姓名:', student.name)
print('学号:', student.id)
print('数学成绩:', student.math)
print('英语成绩:', student.english)
print('语文成绩:', student.chinese)
print('总分:', student.get_total_score())
print('平均分:', student.get_average_score())
else:
print('未找到该学生')
```
输出结果:
```
姓名: 张三
学号: 001
数学成绩: 90
英语成绩: 80
语文成绩: 70
总分: 240
平均分: 80.0
```
阅读全文