写Python代码:设计一套学生成绩管理系统(学生人数至少为10人),要求满足以下要求: (一)程序运行后,需要显示系统主菜单: -主菜单--- 录入学生信息 1- 2、 查找学生信息 3、 删除学生信息 4、 修改学生信息 学生成绩统计 5- 6、 退出系统 (二)按照主菜单内容,对学生成绩进行相关操作并给出对应显示,这里给出示例: 请选择功能:1请输入学生学号:请输入学生姓名: 请输入学生高数成绩:请输入学生英语成绩:请输入学生Python成绩:请输入学生C语言成绩:请输入学生机器学习成绩: 录入完毕?[Y\N]:(输入Y,则询问下一个选择的功能:输入 N,则开始输入下一个学生的信息和成绩。) 在功能2下,则可以根据学生姓名或者是学生学号查找学生信息,并将其所有成绩进行输出显示; 在功能3下,对个别同学的信息进行删除; 在功能4下,可以对所有同学的信息和成绩进行任意修改;在功能5下,需要进行: a) 计算每个学生所有课程的总成绩和平均分, b) 每一门课程中所有学生的平均分 c) 可以选择根据单门课程成绩对学生进行排序,还可 以根据学生5门课程总成绩和平均分进行排序,并对排序结果进行显示。 d)将所有汇总信息保存到一个.csv文件
时间: 2024-03-15 07:42:41 浏览: 94
以下是学生成绩管理系统的Python代码:
```python
import csv
class Student:
def __init__(self, student_id, name, math_score, english_score, python_score, c_score, machine_learning_score):
self.student_id = student_id
self.name = name
self.math_score = math_score
self.english_score = english_score
self.python_score = python_score
self.c_score = c_score
self.machine_learning_score = machine_learning_score
class GradeManagementSystem:
def __init__(self):
self.students = []
def display_menu(self):
print("-----主菜单-----")
print("1、录入学生信息")
print("2、查找学生信息")
print("3、删除学生信息")
print("4、修改学生信息")
print("5、学生成绩统计")
print("6、退出系统")
def add_student(self):
while True:
student_id = input("请输入学生学号:")
name = input("请输入学生姓名:")
math_score = int(input("请输入学生高数成绩:"))
english_score = int(input("请输入学生英语成绩:"))
python_score = int(input("请输入学生Python成绩:"))
c_score = int(input("请输入学生C语言成绩:"))
machine_learning_score = int(input("请输入学生机器学习成绩:"))
student = Student(student_id, name, math_score, english_score, python_score, c_score, machine_learning_score)
self.students.append(student)
is_continue = input("录入完毕?[Y\N]:")
if is_continue.lower() == "n":
continue
else:
break
def search_student(self):
search_type = input("请选择查找方式(1、学号查找 2、姓名查找):")
if search_type == "1":
student_id = input("请输入学生学号:")
for student in self.students:
if student.student_id == student_id:
self.display_student(student)
break
elif search_type == "2":
name = input("请输入学生姓名:")
for student in self.students:
if student.name == name:
self.display_student(student)
break
else:
print("输入有误,请重新选择")
def delete_student(self):
student_id = input("请输入要删除的学生学号:")
for index, student in enumerate(self.students):
if student.student_id == student_id:
del self.students[index]
print("删除成功")
break
def modify_student(self):
student_id = input("请输入要修改的学生学号:")
for student in self.students:
if student.student_id == student_id:
student.name = input("请输入学生姓名:")
student.math_score = int(input("请输入学生高数成绩:"))
student.english_score = int(input("请输入学生英语成绩:"))
student.python_score = int(input("请输入学生Python成绩:"))
student.c_score = int(input("请输入学生C语言成绩:"))
student.machine_learning_score = int(input("请输入学生机器学习成绩:"))
print("修改成功")
break
def calculate_scores(self):
math_scores = []
english_scores = []
python_scores = []
c_scores = []
machine_learning_scores = []
total_scores = []
for student in self.students:
math_scores.append(student.math_score)
english_scores.append(student.english_score)
python_scores.append(student.python_score)
c_scores.append(student.c_score)
machine_learning_scores.append(student.machine_learning_score)
total_score = student.math_score + student.english_score + student.python_score + student.c_score + student.machine_learning_score
total_scores.append(total_score)
avg_score = total_score / 5
student.total_score = total_score
student.avg_score = avg_score
math_avg_score = sum(math_scores) / len(math_scores)
english_avg_score = sum(english_scores) / len(english_scores)
python_avg_score = sum(python_scores) / len(python_scores)
c_avg_score = sum(c_scores) / len(c_scores)
machine_learning_avg_score = sum(machine_learning_scores) / len(machine_learning_scores)
while True:
print("请选择统计方式:")
print("1、计算每个学生所有课程的总成绩和平均分")
print("2、每一门课程中所有学生的平均分")
print("3、根据单门课程成绩对学生进行排序")
print("4、根据学生5门课程总成绩和平均分进行排序")
print("5、返回主菜单")
choice = input("请输入选项:")
if choice == "1":
for student in self.students:
self.display_student(student)
print("总成绩:{}, 平均分:{}".format(student.total_score, student.avg_score))
elif choice == "2":
print("高数平均分:{}, 英语平均分:{}, Python平均分:{}, C语言平均分:{}, 机器学习平均分:{}"
.format(math_avg_score, english_avg_score, python_avg_score, c_avg_score, machine_learning_avg_score))
elif choice == "3":
subject = input("请输入要排序的科目(1、高数 2、英语 3、Python 4、C语言 5、机器学习):")
if subject == "1":
self.students.sort(key=lambda x: x.math_score, reverse=True)
elif subject == "2":
self.students.sort(key=lambda x: x.english_score, reverse=True)
elif subject == "3":
self.students.sort(key=lambda x: x.python_score, reverse=True)
elif subject == "4":
self.students.sort(key=lambda x: x.c_score, reverse=True)
elif subject == "5":
self.students.sort(key=lambda x: x.machine_learning_score, reverse=True)
else:
print("输入有误,请重新选择")
continue
for student in self.students:
self.display_student(student)
elif choice == "4":
sort_type = input("请选择排序方式(1、总成绩排名 2、平均分排名):")
if sort_type == "1":
self.students.sort(key=lambda x: x.total_score, reverse=True)
elif sort_type == "2":
self.students.sort(key=lambda x: x.avg_score, reverse=True)
else:
print("输入有误,请重新选择")
continue
for student in self.students:
self.display_student(student)
elif choice == "5":
break
else:
print("输入有误,请重新选择")
def save_to_file(self):
with open("students.csv", mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["学号", "姓名", "高数成绩", "英语成绩", "Python成绩", "C语言成绩", "机器学习成绩", "总成绩", "平均分"])
for student in self.students:
writer.writerow([student.student_id, student.name, student.math_score, student.english_score,
student.python_score, student.c_score, student.machine_learning_score, student.total_score, student.avg_score])
print("文件保存成功!")
def display_student(self, student):
print("学号:{}, 姓名:{}, 高数成绩:{}, 英语成绩:{}, Python成绩:{}, C语言成绩:{}, 机器学习成绩:{}"
.format(student.student_id, student.name, student.math_score, student.english_score, student.python_score,
student.c_score, student.machine_learning_score))
def run(self):
while True:
self.display_menu()
choice = input("请选择功能:")
if choice == "1":
self.add_student()
elif choice == "2":
self.search_student()
elif choice == "3":
self.delete_student()
elif choice == "4":
self.modify_student()
elif choice == "5":
self.calculate_scores()
elif choice == "6":
self.save_to_file()
break
else:
print("输入有误,请重新选择")
if __name__ == "__main__":
system = GradeManagementSystem()
system.run()
```
该系统可以实现录入学生信息、查找学生信息、删除学生信息、修改学生信息、学生成绩统计等功能,同时还可以将学生信息保存到CSV文件中。
阅读全文