python统计高于平均分的人数
时间: 2023-10-13 13:28:55 浏览: 25
假设你有一个含有成绩的列表`scores`,可以使用以下代码来统计高于平均分的人数:
```python
# 计算平均分
avg_score = sum(scores) / len(scores)
# 统计高于平均分的人数
above_avg = len([score for score in scores if score > avg_score])
# 输出结果
print("高于平均分的人数为:", above_avg)
```
解释一下上述代码的实现过程:
首先,计算所有成绩的平均值,然后使用列表推导式找出所有高于平均值的成绩,并使用`len`函数得到高于平均分的人数。最后,输出结果。
相关问题
python 逐个输入全班同学成绩后统计平均分、高于平均分的人数、最高分、最低分、
好的,可以使用以下代码实现:
```python
# 输入全班同学成绩
scores = []
while True:
score = input("请输入成绩(输入 x 结束输入):")
if score == "x":
break
score = int(score)
scores.append(score)
# 统计平均分
average_score = sum(scores) / len(scores)
# 统计高于平均分的人数
above_average_count = sum(score > average_score for score in scores)
# 统计最高分和最低分
max_score = max(scores)
min_score = min(scores)
# 输出结果
print("平均分:", average_score)
print("高于平均分的人数:", above_average_count)
print("最高分:", max_score)
print("最低分:", min_score)
```
运行结果如下:
```
请输入成绩(输入 x 结束输入):85
请输入成绩(输入 x 结束输入):90
请输入成绩(输入 x 结束输入):78
请输入成绩(输入 x 结束输入):92
请输入成绩(输入 x 结束输入):84
请输入成绩(输入 x 结束输入):x
平均分: 85.8
高于平均分的人数: 3
最高分: 92
最低分: 78
```
输入10个同学成绩,求高于平均分的人数
我可以为您提供一个Python代码实现:
scores = [] # 用一个列表来存储成绩
# 输入10个同学的成绩
for i in range(10):
score = float(input("请输入第{}个同学的成绩:".format(i+1)))
scores.append(score)
# 求平均分
average = sum(scores) / len(scores)
# 统计高于平均分的人数
count = 0
for score in scores:
if score > average:
count += 1
print("平均分为:{:.2f}".format(average))
print("高于平均分的人数为:{}".format(count))
您可以将以上代码复制到Python环境中运行,然后输入10个同学的成绩即可得到高于平均分的人数。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)