答:
直接运行以下代码
import os
class Student:
def __init__(self, id, name, chinese, math, english):
self.id = id
self.name = name
self.chinese = chinese
self.math = math
self.english = english
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def delete_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
break
def update_student(self, id, name, chinese, math, english):
for student in self.students:
if student.id == id:
student.name = name
student.chinese = chinese
student.math = math
student.english = english
break
def display_students(self):
for student in self.students:
print(f"学号:{student.id},姓名:{student.name},语文:{student.chinese},数学:{student.math},英语:{student.english}")
def import_students(self, file_path):
with open(file_path, 'r') as f:
lines = f.readlines()
for line in lines:
id, name, chinese, math, english = line.strip().split(',')
student = Student(id, name, float(chinese), float(math), float(english))
self.add_student(student)
class MainMenu:
def __init__(self):
self.student_manager = StudentManager()
def student_management(self):
while True:
print("1. 添加学生")
print("2. 删除学生")
print("3. 修改学生信息")
print("4. 显示所有学生")
print("5. 导入学生信息")
print("6. 返回主菜单")
choice = input("请输入操作序号:")
if choice == '1':
id = input("请输入学号:")
name = input("请输入姓名:")
chinese = float(input("请输入语文成绩:"))
math = float(input("请输入数学成绩:"))
english = float(input("请输入英语成绩:"))
student = Student(id, name, chinese, math, english)
self.student_manager.add_student(student)
elif choice == '2':
id = input("请输入要删除的学生学号:")
self.student_manager.delete_student(id)
elif choice == '3':
id = input("请输入要修改的学生学号:")
name = input("请输入新的姓名:")
chinese = float(input("请输入新的语文成绩:"))
math = float(input("请输入新的数学成绩:"))
english = float(input("请输入新的英语成绩:"))
self.student_manager.update_student(id, name, chinese, math, english)
elif choice == '4':
self.student_manager.display_students()
elif choice == '5':
file_path = input("请输入文件路径:")
if os.path.exists(file_path):
self.student_manager.import_students(file_path)
else:
print("文件不存在,请检查路径是否正确。")
elif choice == '6':
break
else:
print("输入错误,请重新输入。")
def score_management(self):
while True:
print("1. 查看最低分")
print("2. 查看最高分")
print("3. 返回主菜单")
choice = input("请输入操作序号:")
if choice == '1':
min_score = min([student.chinese + student.math + student.english for student in self.student_manager.students])
print(f"最低分:{min_score}")
elif choice == '2':
max_score = max([student.chinese + student.math + student.english for student in self.student_manager.students])
print(f"最高分:{max_score}")
elif choice == '3':
break
else:
print("输入错误,请重新输入。")
def run(self):
while True:
print("1. 学生管理")
print("2. 成绩管理")
print("3. 退出系统")
choice = input("请输入操作序号:")
if choice == '1':
self.student_management()
elif choice == '2':
self.score_management()
elif choice == '3':
break
else:
print("输入错误,请重新输入。")
if __name__ == "__main__":
main_menu = MainMenu()
main_menu.run()