定义一个数组,存储十个学生的成绩,在定义四个函数,求最高分,最低分,平均分,总分
时间: 2024-10-10 11:10:57 浏览: 82
当然可以。首先,我们需要定义一个数组来存储学生的成绩。假设每个学生的分数是一个浮点数。这是一个简单的例子:
```python
# 定义学生成绩数组
student_scores = [85.0, 92.5, 78.0, 96.5, 88.0, 94.0, 80.0, 91.0, 79.5, 93.5]
# 定义函数求最高分
def get_max_score(scores):
return max(scores)
# 定义函数求最低分
def get_min_score(scores):
return min(scores)
# 定义函数求平均分
def calculate_average(scores):
return sum(scores) / len(scores)
# 定义函数求总分
def total_scores(scores):
return sum(scores)
# 调用函数并打印结果
max_score = get_max_score(student_scores)
min_score = get_min_score(student_scores)
average_score = calculate_average(student_scores)
total_score = total_scores(student_scores)
print(f"最高分:{max_score}")
print(f"最低分:{min_score}")
print(f"平均分:{average_score:.2f}") # 控制小数位数
print(f"总分:{total_score}")
```
这个程序定义了四个函数,分别用于获取最高分、最低分、平均分以及计算总分。
阅读全文
相关推荐


















