Python style百分制转五分制

时间:2025-02-09 07:40:40

百分制转五分制,就是把考试分数用ABCDE表示出来

A 90~100
B 80~89
C 70~79
D 60~69
E 60以下

输入为一个整数,当这个整数不在0~100 的范围内的时候,输出“data error”.


正常的风格是

score=int(input())
if (score > 100 or score < 0):
    print('Data error!')
else:
    if score<=100 and score>=90:
        print("A")
    if score<90 and score>=80:
        print("B")
    if score<80 and score>=70:
        print("C")
    if score<70 and score>=60:
        print("D")
    if score<60:
        print("E")

也可以将五分制构造出一个字符串'EEEEEEDCBAA',用以下方法实现这个功能:

score = int(input())
degree = 'EEEEEEDCBAA'
if (score > 100 or score < 0):
    print('Data error!')
else:
    print(degree[score//10])

这样的方法更加Pythonic