输入一个整数,当输入不在[0,100]区间时输出提示“Data error!”;当输入满足要求的前提下,用多分支结构实现百分制转五分制。实现规则是根据分数所在区间[0,60)、[60,70)、[70,80)、[80,90)、[90,100],分别输出字符“E”、“D”、“C”、“B”、“A”。
# 1.百分转五分多分支方法
score = int(input())
if score > 100 or score < 0:
print('请输入0至100之间的整数!')
elif score >= 90: # 区间为90=<score<=100
print('A')
elif score >= 80: # 区间为80=<score<90
print('B')
elif score >= 70: # 区间为70=<score<80
print('C')
elif score >= 60: # 区间为60=<score<70
print('D')
else: # 区间为0=<score<60
print('E')
# 2.百分制转五分制二分支方法
score = int(input())
if score > 100 or score < 0:
print('请输入0至100之间的整数!')
else: # 此分支下0 =< score <= 100
if score >= 90: # 此分支下90 =< score <= 100
print('A')
else: # 此分支 0 =< score < 90
if score >= 80: # 此分支下80 =< score < 90
print('B')
else: # 此分支 0 =< score < 80
if score >= 70: # 此分支下70 =< score < 80
print('C')
else: # 此分支 0 =< score < 70
if score >= 60: # 此分支下60 =< score < 70
print('D')
else: # 此分支 0 =< score < 60
print('E')
# 3.单分支程序
score = int(input())
# int()函数将输入的整数字符串转换成整数
if 90 <= score <= 100: # 连续比较运算,等价于score >= 90 and score <= 100
print('A')
if 80 <= score < 90:
print('B')
if 70 <= score < 80:
print('C')
if 60 <= score < 70:
print('D')
if 0 <= score < 60:
print('E')
if score < 0 or score > 100:
print('请输入0至100之间的整数!')
# 4.用字符串索引的方法对成绩转换程序进行优化
score = int(input()) # 此处要求输入为非负整数
degree = 'EEEEEEDCBAA' # 字符序号0,1,2,3,4,5,6,7,8,9,10
if score > 100 or score < 0: # 排除不合法数据
print(' Data error!')
else:
i = score // 10 # 索引号必须是整数,此处用整除符号“//”实现
print(degree[i]) # degree[i]是索引操作,返回degree中序号为i的字符
# 方法三
score = float(input()) # 输入为非负数
degree = 'EEEEEEDCBAA' # 字符序号0,1,2,3,4,5,6,7,8,9,10
if score > 100 or score < 0: # 排除不合法数据
print(' Data error!')
else:
i = int(score // 10) # 用int()实现取整使索引号为整数
print(degree[i]) # degree[i]是索引返回degree中序号i的字符
# 方法一,用字符串索引的方法实现成绩转换程序
def hundred_to_five_grade(score):
degree = 'EEEEEEDCBAA' # 字符序号0,1,2,3,4,5,6,7,8,9,10
if score > 100 or score < 0: # 排除不合法数据
return ' Data error!'
else:
i = int(score // 10) # 用int()实现取整使索引号为整数
return degree[i] # degree[i]是索引返回degree中序号i的字符
if __name__ == '__main__':
score_hundred = float(input()) # 输入为非负数
print(hundred_to_five_grade(score_hundred))
# 5.用条件表达式进行优化
score = int(input())
degree = 'EEEEEEDCBAA'
print('Data error!') if (score > 100 or score < 0) else print(degree[score // 10])
# 方法二,用条件表达式实现
def hundred_to_five_grade(score):
degree = 'EEEEEEDCBAA'
print('Data error!') if (score > 100 or score < 0) else print(degree[int(score // 10)])
if __name__ == '__main__':
score_hundred = float(input()) # 输入为非负数
hundred_to_five_grade(score_hundred)
描述
编写一个学生成绩转换程序,用户输入百分制的学生成绩:
成绩大于或等于90且小于或等于100的输出为“A”
成绩大于或等于80且小于90的输出为“B”
成绩大于或等于70且小于80的输出为“C”
成绩大于或等于60且小于70的输出为“D”
成绩小于60的输出为“E”
输入数据不合法时输出“data error!”
用户可反复输入成绩进行转换,直接输入回车时结束程序
输入格式
每次输入一个浮点数,代表百分制成绩;
直接输入回车时结束程序
输出格式
根据每一次的输入值分别输出A、B、C、D、E中的一个字母或”data error!”。
示例
输入:
88
99
56
156
输出:
B
A
E
data error!
常规方法
while True:
score_str = input()
if not score_str: # 直接回车时not score_str 为True
break
score = float(score_str)
if score > 100 or score < 0:
print('data error!')
elif score >= 90:
print('A')
elif score >= 80:
print('B')
elif score >= 70:
print('C')
elif score >= 60:
print('D')
else:
print('E')
while True:
score_str = input()
if not score_str: # 直接回车时not score_str 为True
break
# 此语句功能可以用以下语句实现
while score_str := input(): # 赋值运算,实现循环输入,输入为空时结束循环,非空时进入循环
while score_str := input():
score = float(score_str)
if score > 100 or score < 0:
print('data error!')
elif score >= 90:
print('A')
elif score >= 80:
print('B')
elif score >= 70:
print('C')
elif score >= 60:
print('D')
else:
print('E')
运用字符串、赋值运算符、条件表达式实现
while score_str := input():
score = float(score_str)
degree = 'EEEEEEDCBAA'
print('data error!') if (score > 100 or score < 0) else print(degree[int(score // 10)])