编写一个菜单式的学生成绩分析系统,对某班一门课的成绩进行分析,要求有如下功能:1)查询全部成绩2)查询平均分3)查询不及格率4)查询最高分5)按成绩降序排列0)退出
时间: 2024-02-27 10:57:04 浏览: 23
好的,我了解了你的问题,以下是一个简单的菜单式学生成绩分析系统的代码,你可以参考一下:
```python
class Student:
def __init__(self, name, score):
self.name = name
self.score = score
def __str__(self):
return f"{self.name}: {self.score}"
class ScoreAnalysis:
def __init__(self):
self.students = []
def add_student(self, name, score):
student = Student(name, score)
self.students.append(student)
def query_all(self):
for student in self.students:
print(student)
def query_average(self):
total = 0
for student in self.students:
total += student.score
average = total / len(self.students)
print(f"Average score: {average:.2f}")
def query_fail_rate(self):
fail_count = 0
for student in self.students:
if student.score < 60:
fail_count += 1
fail_rate = fail_count / len(self.students)
print(f"Fail rate: {fail_rate:.2%}")
def query_highest(self):
highest_score = max([student.score for student in self.students])
highest_students = [student for student in self.students if student.score == highest_score]
for student in highest_students:
print(student)
def sort_descending(self):
sorted_students = sorted(self.students, key=lambda student: student.score, reverse=True)
for student in sorted_students:
print(student)
def menu(self):
while True:
print("1. Query all scores")
print("2. Query average score")
print("3. Query fail rate")
print("4. Query highest score")
print("5. Sort by score descending")
print("0. Exit")
choice = input("Please select an option: ")
if choice == "1":
self.query_all()
elif choice == "2":
self.query_average()
elif choice == "3":
self.query_fail_rate()
elif choice == "4":
self.query_highest()
elif choice == "5":
self.sort_descending()
elif choice == "0":
break
else:
print("Invalid option")
if __name__ == "__main__":
score_analysis = ScoreAnalysis()
score_analysis.add_student("Alice", 80)
score_analysis.add_student("Bob", 70)
score_analysis.add_student("Charlie", 90)
score_analysis.add_student("David", 60)
score_analysis.menu()
```
这个系统使用了面向对象的编程风格,将学生和成绩封装到了一个类中,方便进行操作。你可以根据自己的需要进行修改和扩展。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)