用Python语言编写班级成绩统计分析系统软件,完成以下任务:记录学生成绩信息,学生成绩信息结构至少应该包括以下几项:学号、姓名、班级、数学成绩、英语成绩。主要功能包括: 系统的界面菜单; 学生成绩信息输入; (3) 统计每个班级的人数; (4) 分别统计每个班级的数学、英语平均分; (5) 将学生成绩信息按班级存在不同的文本文件(.txt)中,文件名称为“班级名称.txt”,文件中按学生学号排序; (6) 退出程序。
时间: 2024-03-25 16:37:01 浏览: 31
这是一个很好的问题,我为你提供以下参考代码:
```python
import os
class Student:
def __init__(self, id, name, cls, math_score, eng_score):
self.id = id
self.name = name
self.cls = cls
self.math_score = math_score
self.eng_score = eng_score
class GradeAnalyzer:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def get_class_count(self, cls):
count = 0
for student in self.students:
if student.cls == cls:
count += 1
return count
def get_class_avg_score(self, cls, subject):
total_score = 0
count = 0
for student in self.students:
if student.cls == cls:
if subject == "math":
total_score += student.math_score
elif subject == "eng":
total_score += student.eng_score
count += 1
if count == 0:
return 0
else:
return total_score / count
def save_to_file(self):
classes = set([student.cls for student in self.students])
for cls in classes:
students = [student for student in self.students if student.cls == cls]
students.sort(key=lambda student: student.id)
filename = cls + ".txt"
with open(filename, "w") as f:
f.write("学号\t姓名\t班级\t数学成绩\t英语成绩\n")
for student in students:
f.write("{}\t{}\t{}\t{}\t{}\n".format(student.id, student.name, student.cls, student.math_score, student.eng_score))
if __name__ == "__main__":
analyzer = GradeAnalyzer()
while True:
print("1. 添加学生信息")
print("2. 统计班级人数")
print("3. 统计班级数学平均分")
print("4. 统计班级英语平均分")
print("5. 将学生成绩信息按班级存在不同的文本文件中")
print("6. 退出程序")
choice = input("请选择操作:")
if choice == "1":
id = input("请输入学号:")
name = input("请输入姓名:")
cls = input("请输入班级:")
math_score = int(input("请输入数学成绩:"))
eng_score = int(input("请输入英语成绩:"))
student = Student(id, name, cls, math_score, eng_score)
analyzer.add_student(student)
elif choice == "2":
cls = input("请输入班级:")
count = analyzer.get_class_count(cls)
print("班级人数为:{}".format(count))
elif choice == "3":
cls = input("请输入班级:")
avg_score = analyzer.get_class_avg_score(cls, "math")
print("班级数学平均分为:{}".format(avg_score))
elif choice == "4":
cls = input("请输入班级:")
avg_score = analyzer.get_class_avg_score(cls, "eng")
print("班级英语平均分为:{}".format(avg_score))
elif choice == "5":
analyzer.save_to_file()
print("学生成绩信息已保存到文件中")
elif choice == "6":
print("程序已退出")
break
else:
print("无效的选项,请重新选择")
```
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)