要实现成绩的百分制转化为等级制,我们首先要了解多分支选择结构
多分支选择结构的语法为:
if 达式1:
语句块1
elif 表达式2:
语句块2
elif 表达式3:
语句块3
...
else:
语句块n
现在我们就可以输入代码为:
def func(score): if score>100: return'wrong score.must<=100.' elif score>=90: return'A' elif score>=80: return'B' elif score>=70: return'C' elif score>=60: return'D' elif score>=0: return'E' else: return'wrong score.must>0'
结果展示为:
但是这个看上编程有点繁琐,我们可以尝试另一种Python代码来编写:
代码如下:
def func(score): degree='DCBAAE' if score>100 or score<0: return'wrong score.must between 0 and 100' else: index=(score-60)//10 if index>=0: return degree[index] else: return degree[-1]
结果展示为:
今天的学习就到这里啦~