7-1 python-列表:统计考试成绩 分数 10 作者 gllypx 单位 桂林学院 将一组考试成绩通过键盘输入,计算及格率、平均分、最高分和最低分。 输入格式: 成绩在一行输入,数据间用空格分隔。 输出格式: 输出及格率、平均分、最高分、最低分,精确到小数后1位。 见样例 输入样例: 80.5 90 100 45 15 65.5 输出样例: 及格率:66.7% 平均分:66.0 最高分:100.0 最低分:15.0 代码长度限制 16 KB 时间限制 400 ms 内存限制
时间: 2024-03-01 16:51:43 浏览: 16
以下是统计考试成绩的 Python 代码:
```python
scores = list(map(float, input().split()))
# 计算及格率
pass_count = 0
for score in scores:
if score >= 60:
pass_count += 1
pass_rate = pass_count / len(scores)
# 计算平均分
avg_score = sum(scores) / len(scores)
# 计算最高分和最低分
max_score = max(scores)
min_score = min(scores)
# 输出结果
print('及格率:{:.1%}'.format(pass_rate))
print('平均分:{:.1f}'.format(avg_score))
print('最高分:{:.1f}'.format(max_score))
print('最低分:{:.1f}'.format(min_score))
```
代码解析:
1. 首先将输入的字符串转换成浮点数列表,使用 `map()` 函数和 `split()` 方法实现。
2. 遍历成绩列表,统计及格人数,并计算及格率。
3. 使用 `sum()` 函数和列表长度计算平均分。
4. 使用 `max()` 和 `min()` 函数分别计算最高分和最低分。
5. 使用格式化字符串输出结果,保留一位小数并将及格率转换成百分数形式。
相关问题
7-1 python-列表:统计考试成绩
好的,我来回答你的问题。
统计考试成绩可以通过 Python 中的列表进行操作。我们可以将考试成绩存储在一个列表中,并使用内置函数对其进行统计。以下是一个简单的示例代码:
```python
# 定义一个存储成绩的列表
scores = [85, 76, 92, 68, 78, 89, 91, 95, 72, 87]
# 计算总分
total_score = sum(scores)
# 计算平均分
average_score = total_score / len(scores)
# 计算最高分
highest_score = max(scores)
# 计算最低分
lowest_score = min(scores)
# 输出统计结果
print("总分:", total_score)
print("平均分:", average_score)
print("最高分:", highest_score)
print("最低分:", lowest_score)
```
这段代码会输出以下结果:
```
总分: 813
平均分: 81.3
最高分: 95
最低分: 68
```
当然,如果你有更多的需求,还可以使用其他的 Python 库和函数来进行更加复杂的统计操作。
python 将一组考试成绩通过键盘输入,计算及格率、平均分、最高分和最低分。
可以使用以下代码实现:
```python
scores = [] # 用于存储输入的成绩
# 循环输入成绩,输入-1表示结束输入
while True:
score = float(input("请输入一个成绩(输入-1结束输入):"))
if score == -1:
break
scores.append(score)
# 计算及格率
pass_count = 0
for score in scores:
if score >= 60:
pass_count += 1
pass_rate = pass_count / len(scores) * 100
# 计算平均分
average_score = sum(scores) / len(scores)
# 计算最高分和最低分
max_score = max(scores)
min_score = min(scores)
# 输出结果
print("及格率为:{:.2f}%".format(pass_rate))
print("平均分为:{:.2f}".format(average_score))
print("最高分为:{:.2f}".format(max_score))
print("最低分为:{:.2f}".format(min_score))
```
这个程序会循环输入成绩,输入-1表示结束输入。然后计算及格率、平均分、最高分和最低分,并输出结果。注意,这里使用了 `float()` 函数将输入的字符串转换成了浮点数,以便进行计算。