Python:成绩分类

输入一组用百分制(0-100之间的整数)表示的成绩列表,每个成绩用空格隔开,将其转换为等级制并进行各等级的统计。

0<=分数<60为F 60<=分数<70为D

70<=分数<80为C

80<=分数<90为B

90<=分数<=100为A

先介绍第一种方法:

str=list(input().split())
str2={}
print(str)
a='A:'
b='B:'
c='C:'
d='D:'
f='F:'
str2[a]=str2.get(a,0)
str2[b]=str2.get(b,0)
str2[c]=str2.get(c,0)
str2[d]=str2.get(d,0)
str2[f]=str2.get(f,0)
for item in str:
    item=float(item)
    if 0<item and item<60:
        x='F:'
        str2[x]=str2.get(x,0)+1
    if 60<= item and item <70:
        x='D:'
        str2[x] = str2.get(x, 0) + 1
    if 70<=item and item<80:
        x='C:'
        str2[x]=str2.get(x,0)+1
    if 80<=item and item<90:
        x='B:'
        str2[x]=str2.get(x,0)+1
    if 90<=item and item<100:
        x='A:'
        str2[x]=str2.get(x,0)+1
str2= sorted(str2.items(),key=lambda x:x[0])
str2={k:v for k,v in str2}
print(str2)
for a,b in str2.items():
    print(a,':'," ",b)

这里面用到了数据字典,然后将数据字典进行了排序:

str2= sorted(str2.items(),key=lambda x:x[0])

再将排序后的列表转换成字典样式

str2={k:v for k,v in str2}

第二种方法:

str = input()
str = str + ' '
sum=0
flag = 0
dj=[0,0,0,0,0]
for x in str :
    if x==' ' and flag == 1:
        if sum >= 90 and sum <= 100:
            dj[0]+=1
        elif sum >= 80 and sum < 90 :
            dj[1]+=1
        elif sum >= 70 and sum < 80 :
            dj[2]+=1
        elif sum >= 60 and sum < 70 :
            dj[3]+=1
        elif sum >= 0 and sum <60  :
            dj[4]+=1
        sum = 0
        flag = 0
    elif x != ' ' :
        a=int(x)
        sum=sum*10+a
        flag = 1
print("A: %d"%dj[0])
print("B: %d"%dj[1])
print("C: %d"%dj[2])
print("D: %d"%dj[3])
print("F: %d"%dj[4])

第三种方法:

thisdict={"A": 0,"B": 0,"C": 0,"D": 0,"F": 0}
a=list(map(int,input().split()))
for x in a:
    if 90<=x<=100:
        thisdict["A"]=thisdict.get("A")+1;
    elif 80<=x<90:
        thisdict["B"]=thisdict.get("B")+1;
    elif 70<=x<80:
        thisdict["C"]=thisdict.get("C")+1;
    elif 60<=x<70:
        thisdict["D"]=thisdict.get("D")+1;
    elif 0<=x<60:
        thisdict["F"]=thisdict.get("F")+1;
for x, y in thisdict.items():
    print("%c: %d"%(x,y))

猜你喜欢

转载自blog.csdn.net/qq_45801904/article/details/123643538