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就可以。