统计分数、生成期末成绩

在统计分数和生成期末成绩时遇到问题,发现使用`int`和`round()`函数在处理90分边界时有差异。答案中使用`int`函数,而我使用`round()`,导致90分及以上成绩的处理不一致。注意到90分不包含在90分以上范围内,这样的文字游戏导致结果偏差。虽然答案更简洁,但也暴露出在处理边界条件时的细节问题。
f = open('C:\\ecnu_ks\\root\\score1.txt','r')
n = open('C:\\ecnu_ks\\root\\score3.txt','w')
slist = []
scorelist = []
outs = ''
while True:
    line = f.readline()
    if line == '':
        break
    elif line =='学号 平时成绩 期末成绩\n':
        continue
    else:
        slist.append(line.split())

for peo in slist:
    for i in range(1,2+1):
        peo[i] = int(peo[i])
        score = round(0.4*int(peo[1])+0.6*int(peo[2]))

    scorelist.append(score)
    peosco = peo[0]+'\t'+str(score)+'\n'
    outs += peosco
outs = '学号\t总评成绩\n'+outs
n.write(outs)
f.close()
n.close()
print(scorelist)
countlist = [0,0,0,0,0]
for score in scorelist:
    if score>=90:
        countlist[0] += 1
    elif score>=80:
        countlist[1] += 1
    elif score>=70:
        countlist[2] += 1
    elif score>=60:
        countlist[3] += 1
    else:
        countlist[4] += 1
print(countlist)
renshu = len(scorelist)
zongfen = sum(scorelist)
av = zongfen/renshu

print('学生总人数为%d'%renshu,',','按总评成绩计,90以上%d'%countlist[0],'人','、','80~89间%d'%countlist[1],'人','、','70~79间%d'%countlist[2],'人','、',end = '')
print('60~69间%d'%countlist[3],'人','、','60分以下%d'%countlist[4],'人','。', end = '')
print('班级总平均分为%d'%av,'分。')
            

我写的。我也不知道为什么这么多这么吓人。

f=open("C:\\ecnu_ks\\root\\score1.txt")
a=f.readlines()
del a[0]
L3=[]
for line in a:
    L1=line.split()
    f_score=int(int(L1[1])*0.4+int(L1[2])*0.6)
    L3.append([L1[0],f_score])
f.close()
c=[0,0,0,0,0]
count=0
sum=0
f2=open("C:\\ecnu_ks\\root\\score2.txt",'w')
f2.write("学号 平均成绩\n");
for L2 in L3:
    if 90<L2[1]<=100:
        c[0]+=1
    elif L2[1]>=80:
        c[1]+=1
    elif L2[1]>=70:
        c[2]+=1
    elif L2[1]>=60:
        c[3]+=1
    else:
        c[4]+=1
    count+=1
    sum+=L2[1]
    f2.write(L2[0]+" "+str(L2[1])+"\n")
f2.close()
avg_score=int(sum/count)
print("学生总人数为%d,按总评成绩计,90以上%d人、80~89间%d人、70~79间%d人、60~69间%d人、60分以下%d人。班级总平均分为%d分。"%(count,c[0],c[1],c[2],c[3],c[4],avg_score))

答案上的。我们算出来的结果不一样,看了几个数据,发现是……

int和round()函数。

题目要求取整数,所以我用了round(),而答案用的是int。这和给出的目标范例是一样的。

但是结果在九十分的这两侧,差了一个人。后来发现,九十分以上不包括九十分。。。

这种文字游戏玩得真累。90分的同学里外不是人。


不过,答案还是要比我的简洁很多的。刚开始我的就多了好几行,本来用一个del就可以。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值