7. 列出所有学生信息功能的实现
实现方法
- 班级相关模块:
class_room.py
import student
from student import Student
import tools
class ClassRoom:
'''班级类型'''
def __init__(self, class_name):
self.class_name = class_name # 班级名称
self.student = [] # 保存学生信息
...
def list_all_student_info(self, student_list):
'''显示所有学生的信息'''
print('+------+----------------------+--------+--------+')
print('| 序号 | 姓名 |语文成绩|数学成绩|')
print('+------+----------------------+--------+--------+')
number = 1
for stu in student_list:
print('| %4d | %s | %4d | %4d |' % (
number, tools.center_to_display_width(stu.name, 20),
stu.chinese_score, stu.math_score))
number += 1
if len(student_list):
print('+------+----------------------+--------+--------+')
...
def student_manager(self):
'''此函数用来学生数据'''
while True:
self.show_class_menu()
sel = input('请选择:')
match sel:
case '1': # 1) 添加学生
self.add_student()
case '2': # 2) 修改学生的语文成绩
pass
case '3': # 3) 修改学生的数学成绩
pass
case '4': # 4) 删除学生
pass
case '5': # 5) 列出所有学生的成绩
self.list_all_student_info(self.student)
case '6': # 6) 按语文成绩从高到低显示学生成绩
...
if __name__ == '__main__':
cr1 = ClassRoom('一年1班')
cr1.student.append(Student('张三', 100, 61))
cr1.student.append(Student('李四', 70, 81))
cr1.student.append(Student('王五', 90, 71))
cr1.student.append(Student('赵六', 80, 91))
cr1.student_manager()