python: 设计一个程序,运行时输入班级人数,根据人数输入这个班级学生的成绩,输出学生成绩,计算总分和平均分。再将低于平均分的人数统计出来,并对学生成绩进行从高到低排序。
时间: 2024-10-23 14:02:23 浏览: 12
python 输入学生考试成绩计算平均分 示例
5星 · 资源好评率100%
在Python中,你可以设计一个简单的命令行程序来满足这些需求。首先,我们需要定义一个`Student`类来存储姓名和成绩,然后创建一个函数来获取输入并处理数据。下面是一个示例:
```python
class Student:
def __init__(self, name, score):
self.name = name
self.score = score
def get_student_data(num_students):
students = []
for i in range(num_students):
name = input(f"请输入第{i+1}名学生的姓名:")
score = float(input(f"请输入第{i+1}名学生的分数:"))
students.append(Student(name, score))
return students
def calculate_average(students):
total_score = sum(student.score for student in students)
average_score = total_score / len(students)
return total_score, average_score
def filter_below_average(students, average_score):
below_average = [student for student in students if student.score < average_score]
return below_average
def sort_scores(students):
sorted_students = sorted(students, key=lambda s: s.score, reverse=True)
return sorted_students
def main():
num_students = int(input("请输入班级人数:"))
students = get_student_data(num_students)
total_score, average_score = calculate_average(students)
print(f"总分:{total_score}, 平均分:{average_score}")
below_average_count = len(filter_below_average(students, average_score))
print(f"低于平均分的学生有 {below_average_count} 人")
print("学生成绩从高到低排序:")
for student in sort_scores(students):
print(f"{student.name}: {student.score}")
if __name__ == "__main__":
main()
```
当你运行此程序时,它会提示你依次输入每个学生的姓名和分数,最后显示总分、平均分,以及低于平均分的学生数量和学生成绩的排序结果。
阅读全文