To achieve the percentile score into a hierarchy, we must first understand the structure of a multi-branch selection
Multi-branch selection structure syntax is:
if expression 1:
Statement block 1
elif expression 2:
Statement block 2
elif expression 3:
Statement block 3
...
else:
Statement block n
Now we can enter the code:
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'
The results are shown as follows:
But this fancy little tedious programming, we can try another Python code to write:
code show as below:
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]
The results are shown as follows:
Today's study on here friends ~