利用自定义函数,统计平均成绩、中位数、最高成绩、最低成绩
时间: 2023-07-10 12:32:16 浏览: 63
好的,下面是利用自定义函数在Python中实现统计平均成绩、中位数、最高成绩、最低成绩的代码:
```python
def get_statistics(scores):
# 统计平均成绩
avg_score = sum(scores) / len(scores)
# 统计中位数
sorted_scores = sorted(scores)
n = len(sorted_scores)
if n % 2 == 0:
median_score = (sorted_scores[n//2-1] + sorted_scores[n//2]) / 2
else:
median_score = sorted_scores[n//2]
# 统计最高成绩和最低成绩
max_score = max(scores)
min_score = min(scores)
# 返回统计结果
return avg_score, median_score, max_score, min_score
```
这个函数接受一个成绩列表作为参数,然后返回四个统计结果:平均成绩、中位数、最高成绩和最低成绩。你可以根据自己的需要调用这个函数,并传入你的成绩列表。
相关问题
利用自定义函数,统计平均成绩、中位数、最高成绩、最低成绩。
好的,我这里编写了一个 Python 函数来计算平均成绩、中位数、最高成绩和最低成绩。函数的实现如下:
```python
import random
def calculate_scores(num_students):
# 生成随机的成绩
scores = [random.randint(60, 100) for _ in range(num_students)]
# 计算平均成绩
avg_score = sum(scores) / num_students
# 计算中位数
sorted_scores = sorted(scores)
mid_index = num_students // 2
if num_students % 2 == 0:
median = (sorted_scores[mid_index-1] + sorted_scores[mid_index]) / 2
else:
median = sorted_scores[mid_index]
# 计算最高成绩和最低成绩
max_score = max(scores)
min_score = min(scores)
# 打印结果
print("平均成绩:", avg_score)
print("中位数:", median)
print("最高成绩:", max_score)
print("最低成绩:", min_score)
```
这个函数接受一个整数参数 `num_students`,表示学生的人数。函数会随机生成 `num_students` 个成绩,并计算出平均成绩、中位数、最高成绩和最低成绩,并打印出来。
你可以调用这个函数来测试它的功能,例如:
```python
calculate_scores(10)
```
这个函数会生成 10 个随机成绩,并输出平均成绩、中位数、最高成绩和最低成绩。
利用列表存储数据,统计平均成绩、中位数、最高三个成绩、最低三个成绩。
好的,我可以给出一个参考的代码实现。假设有一个名为`scores`的列表,保存了学生成绩,可以按照如下方式进行统计:
```python
scores = [88, 92, 95, 72, 86, 74, 90, 92, 78, 85]
# 平均成绩
avg_score = round(sum(scores) / len(scores), 2)
print("平均成绩:", avg_score)
# 中位数
sorted_scores = sorted(scores)
n = len(sorted_scores)
if n % 2 == 0:
median = (sorted_scores[n // 2 - 1] + sorted_scores[n // 2]) / 2
else:
median = sorted_scores[n // 2]
print("中位数:", median)
# 最高三个成绩
top3_scores = sorted_scores[-3:]
print("最高三个成绩:", top3_scores)
# 最低三个成绩
bottom3_scores = sorted_scores[:3]
print("最低三个成绩:", bottom3_scores)
```
输出结果为:
```
平均成绩: 85.0
中位数: 86
最高三个成绩: [90, 92, 95]
最低三个成绩: [72, 74, 78]
```
这里的中位数计算使用了Python内置的`sorted()`函数,该函数可以将一个列表按照升序排序。至于如何计算中位数,可以根据列表长度的奇偶性进行分类讨论。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)