随机生成50个学生的三门课程成绩,请分别计算并输出每一门课程的最高分,最低分和平均分
时间: 2024-09-21 11:01:57 浏览: 34
首先,假设我们有一个简单的数据结构,例如学生信息包含姓名和三门课程的成绩。我们可以使用Python来随机生成这个数据,并进行统计分析。这里是一个简单的示例:
```python
import random
# 定义一个学生类
class Student:
def __init__(self, name):
self.name = name
self.course_scores = {'Math': random.randint(60, 100), 'English': random.randint(60, 100), 'Science': random.randint(60, 100)}
# 创建50个学生
students = [Student(f"Student_{i}") for i in range(50)]
# 计算每门课程的最高分、最低分和平均分
def calculate_stats(scores):
max_score, min_score, total = None, float('inf'), 0
for student_scores in scores.values():
for score in student_scores.values():
if not max_score or score > max_score:
max_score = score
if not min_score or score < min_score:
min_score = score
total += score
average_score = total / len(scores)
return max_score, min_score, average_score
# 分别对数学、英语和科学进行统计
math_max, math_min, math_avg = calculate_stats({student.course_scores['Math'] for student in students})
english_max, english_min, english_avg = calculate_stats({student.course_scores['English'] for student in students})
science_max, science_min, science_avg = calculate_stats({student.course_scores['Science'] for student in students})
print(f"Math:")
print(f"最高分: {math_max}, 最低分: {math_min}, 平均分: {math_avg:.2f}")
print(f"English:")
print(f"最高分: {english_max}, 最低分: {english_min}, 平均分: {english_avg:.2f}")
print(f"Science:")
print(f"最高分: {science_max}, 最低分: {science_min}, 平均分: {science_avg:.2f}")
阅读全文