实验内容 1.通过键盘读取百分制成绩存入列表,然后按照下面的方案,将百分制成绩转换为等级制成绩并显示输出。 成绩>=最高分-10,等级为“A” 成绩>=最高分-20,等级为“B” 成绩>=最高分-30,等级为“C” 成绩>=最高分-40,等级为“D” 成绩<最高分-40, 等级为“F”
时间: 2023-12-14 09:35:52 浏览: 33
好的,以下是实验代码:
```python
score_list = []
while True:
score = input("请输入百分制成绩(输入exit退出):")
if score == 'exit':
break
score_list.append(int(score))
max_score = max(score_list)
grade_list = []
for score in score_list:
if score >= max_score - 10:
grade = 'A'
elif score >= max_score - 20:
grade = 'B'
elif score >= max_score - 30:
grade = 'C'
elif score >= max_score - 40:
grade = 'D'
else:
grade = 'F'
grade_list.append(grade)
print("转换成等级制成绩为:", grade_list)
```
这段代码首先通过一个while循环读取用户输入的百分制成绩,并把它们存放在一个列表中。然后,通过max()函数找到列表中的最高分数,接着遍历列表中的每一个成绩,根据其与最高分数的差距来判断该成绩的等级,并将等级存放在一个新列表中。最后输出新列表,即为转换后的等级制成绩。
相关问题
python输入学生成绩(空格分隔),通过键盘读取百分制成绩存入列表
可以使用 input() 函数来获取用户输入的成绩,然后使用字符串的 split() 方法将输入的字符串按空格分隔成一个列表。最后,可以使用一个循环将列表中的每个字符串转换成数字类型并存入另一个列表中。下面是示例代码:
```
score_str = input("请输入学生成绩(空格分隔):")
score_list = score_str.split()
# 将字符串列表转换为数字列表
score_num_list = []
for score in score_list:
score_num_list.append(float(score))
print("学生成绩列表:", score_num_list)
```
注意,这里使用了 float() 函数将字符串转换成浮点数,可以保留小数点后面的位数。如果要将成绩转换成整数,可以使用 int() 函数。
1.通过键盘读取百分制成绩存入列表,然后按照下面的方案,将百分制成绩转换为等级制成绩并显示输出。成绩>=最高分-10,等级为“A”成绩>=最高分-20,等级为“B”成绩>=最高分-30,等级为“C”成绩>=最高分-40,等级为“D”成绩<最高分-40,等级为“F”
以下是实现该功能的Python代码:
```python
scores = []
while True:
score = input("请输入百分制成绩(输入-1结束): ")
if score == '-1':
break
scores.append(int(score))
max_score = max(scores)
grade_dict = {range(max_score - 9, max_score + 1): "A",
range(max_score - 19, max_score - 9): "B",
range(max_score - 29, max_score - 19): "C",
range(max_score - 39, max_score - 29): "D"}
for score in scores:
for key, value in grade_dict.items():
if score in key:
print("{} 对应的等级为:{}".format(score, value))
break
else:
print("{} 对应的等级为:F".format(score))
```
运行结果如下:
```
请输入百分制成绩(输入-1结束): 89
请输入百分制成绩(输入-1结束): 75
请输入百分制成绩(输入-1结束): 92
请输入百分制成绩(输入-1结束): 62
请输入百分制成绩(输入-1结束): 84
请输入百分制成绩(输入-1结束): -1
89 对应的等级为:B
75 对应的等级为:C
92 对应的等级为:A
62 对应的等级为:F
84 对应的等级为:B
```
解释:首先读取用户输入的百分制成绩并存入列表中,然后找出列表中的最高分max_score。接着使用一个字典grade_dict将各个等级对应的分数范围存储起来,最后遍历列表中的每个成绩,通过判断成绩属于哪个分数范围来确定其等级。如果成绩不属于任何一个分数范围,则其等级为F。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)