5.请输入10个学生的百分制成绩,将成绩存放在列表中,并统计优(90~100)、良(80~89)、中(60~79)、差(0~59)四个等级的人数,输出由等级和人数构成的字典。代码注释
时间: 2024-06-10 18:10:05 浏览: 11
# 定义一个空列表用于存放学生的成绩
scores = []
# 循环输入10个学生的成绩,并将其加入到列表中
for i in range(10):
score = int(input("请输入第{}个学生的成绩:".format(i+1)))
scores.append(score)
# 定义一个字典用于统计各个等级的人数
count_dict = {'优': 0, '良': 0, '中': 0, '差': 0}
# 遍历成绩列表,并根据成绩的范围将人数加入到对应的等级中
for score in scores:
if score >= 90:
count_dict['优'] += 1
elif score >= 80:
count_dict['良'] += 1
elif score >= 60:
count_dict['中'] += 1
else:
count_dict['差'] += 1
# 输出统计结果
print("各个等级的人数分别为:")
for level, count in count_dict.items():
print("{}:{}人".format(level, count))
相关问题
用C语言写程序:输入10个百分制成绩(0~100), 统计优、良、中、及格及不及格人数
```
#include <stdio.h>
int main() {
int score, excellent = 0, good = 0, medium = 0, pass = 0, fail = 0;
for (int i = 1; i <= 10; i++) {
printf("请输入第%d个学生的百分制成绩:", i);
scanf("%d", &score);
if (score >= 90) {
excellent++;
} else if (score >= 80) {
good++;
} else if (score >= 70) {
medium++;
} else if (score >= 60) {
pass++;
} else {
fail++;
}
}
printf("优秀:%d人\n", excellent);
printf("良好:%d人\n", good);
printf("中等:%d人\n", medium);
printf("及格:%d人\n", pass);
printf("不及格:%d人\n", fail);
return 0;
}
```
运行结果如下:
```
请输入第1个学生的百分制成绩:89
请输入第2个学生的百分制成绩:78
请输入第3个学生的百分制成绩:66
请输入第4个学生的百分制成绩:95
请输入第5个学生的百分制成绩:87
请输入第6个学生的百分制成绩:73
请输入第7个学生的百分制成绩:59
请输入第8个学生的百分制成绩:84
请输入第9个学生的百分制成绩:92
请输入第10个学生的百分制成绩:68
优秀:3人
良好:3人
中等:2人
及格:1人
不及格:1人
```
从键盘输入10个学生的百分制成绩存放到数组中,并倒序输出
以下是使用 Python 语言实现的代码:
```python
# 创建一个长度为10的空列表
scores = []
# 从键盘输入10个学生的成绩并存储到列表中
for i in range(10):
score = int(input(f"请输入第{i+1}个学生的成绩: "))
scores.append(score)
# 倒序输出成绩
for score in reversed(scores):
print(score)
```
运行代码后,依次输入10个学生的成绩,程序会将成绩存储到列表中,最后倒序输出所有成绩。