4.scores.txt文件存放着某班学生的计算机成绩,包含学号、平时成绩、期末成绩三列。请根据平时成绩占40%,期末成绩占60%的比例计算总评成绩,并按学号、总评成绩两列写入另一个文件scored.txt中。同时在屏幕上输出学生总人数,按总评成绩计算90分以上、80~89分、70~79分、60~69分、60分以下各成绩区间的人数和班级总平均分(取小数点后两位)。
f = open("score1.txt",encoding='gb18030', errors='ignore')
a = f.readlines()
del a[0]
L3=[]
for line in a:
line=line.strip()
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]
count=0
sum=0
f2 = open("score2.txt",'w')
f2.write("学号 平均成绩\n");
for L2 in L3:
if 90<f_score <=100:
c[0] += 1
elif f_score >=80:
c[1] += 1
elif f_score >=70:
c[2] += 1
elif f_score >=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-90有%d人,70-80有%d人,60-70有%d人,60以下有%d人,班级总平均分%d分。"%d(count,c[0],c[1],c[2],c[3],c[4],avg_score))
运行出现Traceback (most recent call last):
File "C:\Users\user\Desktop\T4.py", line 9, in
f_score=int(int(L1[1])*0.4+int(L1[2])*0.6)
IndexError: list index out of range