Grady_1234
2019-10-31 09:34出现list index out of range错误如何解决?
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
- 点赞
- 回答
- 收藏
- 复制链接分享
2条回答
为你推荐
- 如何解析TOML中的嵌套数组/子表?
- struct
- 1个回答
- 实现操作系统页面置换算法,调用的ListUtils.java中的代码都是什么意思?能否帮忙解释一下原理?
- java
- eclipse
- 2个回答
- Python的list.pop()方法的Go惯用法是什么?
- slice
- list
- 5个回答
- python复制改的别人代码,IndexError: list index out of range报错,实在找不到根源在哪儿
- python
- 2个回答
- MSSQL_CONNECT什么也不返回 - 没有错误但没有响应
- iis
- php
- 3个回答
换一换