随机生成十个学生的成绩,判断这十个学生成绩的等级(90分以上为A,80~90为B,70分以下为C)
时间: 2023-05-28 22:07:31 浏览: 74
以下是Python代码实现:
```python
import random
# 生成随机成绩
scores = [random.randint(0, 100) for i in range(10)]
# 判断等级并输出结果
for score in scores:
if score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("C")
```
运行结果示例:
```
B
C
B
C
A
B
B
C
A
B
```
相关问题
用函数实现随机生成十个学生的成绩,判断这十个学生成绩的等级(90分以上为A,80~90为B,70分以下为C) 点击复制后,将打开C知道体验页
import random
def generate_scores():
scores = []
for i in range(10):
score = random.randint(0, 100)
scores.append(score)
return scores
def judge_level(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
else:
return 'C'
scores = generate_scores()
print('十个学生的成绩:', scores)
levels = []
for score in scores:
level = judge_level(score)
levels.append(level)
print('十个学生的等级:', levels)
随机生成20个学生的成绩,并判断这20个学生成绩的等级A:90~100;B:80~90;C:70~80;D:<70
好的,这里是一个随机生成20个学生的成绩,并判断成绩等级的Python程序:
```python
import random
# 生成20个学生的成绩
scores = [random.randint(0, 100) for _ in range(20)]
# 打印成绩
print("成绩:", scores)
# 判断成绩等级
grades = []
for score in scores:
if score >= 90:
grades.append("A")
elif score >= 80:
grades.append("B")
elif score >= 70:
grades.append("C")
else:
grades.append("D")
# 打印成绩等级
print("成绩等级:", grades)
```
在这个程序中,我们首先使用列表推导式生成20个随机成绩,并打印出来。然后,我们使用循环遍历每个成绩,并根据成绩的大小判断成绩等级。最后,我们将每个学生的成绩等级存储在另一个列表中,并打印出来。
运行程序后,输出将类似于这样:
```
成绩: [63, 80, 22, 82, 53, 60, 89, 50, 92, 1, 85, 16, 72, 4, 46, 81, 9, 4, 50, 60]
成绩等级: ['D', 'B', 'D', 'B', 'D', 'D', 'B', 'D', 'A', 'D', 'B', 'D', 'C', 'D', 'D', 'B', 'D', 'D', 'D', 'D']
```
这说明我们成功地生成了20个随机成绩,并判断了每个学生的成绩等级。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)