从若干学生成绩中统计高于(严格的大于)平均分的人数,用-1做为学生成绩数据的结束标志 【输入形式】一组学生的成绩 【输出形式】高于平均分的学生人数 【样例输入】70 50 80 -1 【样例输出】2
时间: 2023-11-29 18:05:39 浏览: 72
明白了,这是一道简单的统计题目。以下是 Python 代码实现:
```python
scores = []
while True:
score = int(input())
if score == -1:
break
scores.append(score)
avg = sum(scores) / len(scores)
count = 0
for score in scores:
if score > avg:
count += 1
print(count)
```
输入学生成绩,将其存储在列表 `scores` 中。当输入 `-1` 时,跳出输入循环。计算平均分 `avg`,遍历列表 `scores` 统计高于平均分的人数 `count`。最后输出 `count` 即可。
相关问题
python从若干学生成绩中统计高于(严格的大于)平均分的人数,用-1做为学生成绩数据的结束标志
可以尝试以下代码实现:
```python
scores = []
while True:
score = input("请输入学生成绩(-1结束):")
if score == "-1":
break
scores.append(int(score))
average = sum(scores) / len(scores)
count = 0
for score in scores:
if score > average:
count += 1
print("高于平均分的人数为:", count)
```
解释一下代码:
1. 首先定义一个空列表 `scores`,用于存储输入的学生成绩;
2. 通过 `while` 循环不断接收用户输入的学生成绩,直到输入 `-1` 结束循环;
3. 计算所有学生成绩的平均分 `average`;
4. 遍历列表 `scores`,如果某个学生成绩高于平均分,则将计数器 `count` 加 1;
5. 输出最终结果。
用python编写一个程序,输入若干学生成绩,统计高于平均分的人数。输入成绩用空格分隔,用-1做为输入成绩数据的结束标志。
好的,这道题目很简单,可以用以下代码实现。
```python
scores = []
while True:
score = input("请输入学生成绩:")
if score == "-1":
break
scores.append(float(score))
avg_score = sum(scores) / len(scores)
num_above_avg = sum(1 for score in scores if score > avg_score)
print("平均分是:", avg_score)
print("高于平均分的人数是:", num_above_avg)
```
希望对你有帮助!
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.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)