python 输入一个百分制成绩,要求输出成绩等级A、B、C、D、E,其中90-100分为A,80-89分为B,70-79分为C,60-69分为D,60分以下为E。(要求用字典做)
时间: 2023-06-06 08:06:42 浏览: 416
非常感谢您的提问。根据您的问题描述,我可以回答:
请看以下代码:
```
score = int(input("请输入成绩:"))
grade_dict = {10: "A", 9: "A", 8: "B", 7: "C", 6: "D", 5: "E", 4: "E", 3: "E", 2: "E", 1: "E", 0: "E"}
grade = grade_dict[int(score/10)]
print(f"成绩等级为:{grade}")
```
以上代码利用Python内置函数 input() 获取用户输入百分制成绩,并通过字典 grade_dict 匹配等级。其中,字典 grade_dict 以分数为键,等级为值。最后,代码利用 fstring 把结果以指定格式输出。
希望这个答案对您有所帮助。如果您有任何其他问题,请随时问我,谢谢!
相关问题
用python写一个程序 1. 输入一个百分制成绩,要求输出成绩等级A、B、C、D、E,其中 90~100分 为A, 80~89分 为B, 70~79分 为C, 60~69 分为D,60分以下为E。
### 回答1:
# 示例代码
score = int(input('请输入一个百分制成绩:'))
if score >= 90 and score <= 100:
print('A')
elif score >= 80 and score <= 89:
print('B')
elif score >= 70 and score <= 79:
print('C')
elif score >= 60 and score <= 69:
print('D')
else:
print('E')
### 回答2:
您好!以下是用Python编写的程序:
```python
score = float(input("请输入百分制成绩:"))
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "E"
print("成绩等级为:", grade)
```
运行该程序后,会要求您输入一个百分制成绩,然后根据规定的分数范围,判断成绩等级并输出。例如,如果您输入85,程序会输出成绩等级为B。如果输入的成绩不在0~100之间,程序也会进行合理的处理,例如输入110会被视为输入错误,程序不会输出等级。希望以上内容对您有帮助!
### 回答3:
可以用以下代码实现:
```python
score = int(input("请输入百分制成绩:"))
if score >= 90 and score <= 100:
print("成绩等级为:A")
elif score >= 80 and score < 90:
print("成绩等级为:B")
elif score >= 70 and score < 80:
print("成绩等级为:C")
elif score >= 60 and score < 70:
print("成绩等级为:D")
else:
print("成绩等级为:E")
```
首先,使用`input`函数接收用户输入的百分制成绩,并将其转换为整数类型。
然后,利用`if-elif-else`语句根据不同的分数段输出相应的成绩等级。判断条件根据题目要求设置,满足条件则输出相应的成绩等级。
最后,使用`print`函数将成绩等级输出到屏幕上。
输入一个百分制成绩,要求输出成绩等级A、B、C、D、E。其中90-100分为A,80-89为B,70-79为C,60-69为D,60分以下为 E。要求: (1)分别用if 语句和switch语句实现。 (2)输入百分制成绩后要判断该成绩的合理性,对不合理的成绩输出出错信息。
当然,这里我会演示如何分别用`if`语句和`switch`语句来实现这个成绩等级转换的功能,并包含输入检查部分。
**使用if语句实现:**
```python
def grade_if(score):
if score < 0 or score > 100:
return "错误:成绩输入不合理"
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else:
return 'E'
score = float(input("请输入百分制成绩:"))
print(grade_if(score))
```
**使用switch语句(在一些支持switch的编程语言如Python的`if-elif-else`结构类似,但Python本身不直接支持switch):**
```python
def grade_switch(score):
if score < 0 or score > 100:
return "错误:成绩输入不合理"
grades = {
100: 'A',
90: 'A',
80: 'B',
70: 'C',
60: 'D',
}
for grade, boundary in grades.items():
if score >= boundary:
return grade
print(grade_switch(float(input("请输入百分制成绩:"))))
```
阅读全文
相关推荐
















