Nakerill 2022-04-28 19:01 采纳率: 50%
浏览 41
已结题

为什么这个有时候可以运行,有时候会出错呢?

img

这个有message.txt文件,有时候可以运行成功,代码也不动一下,有时候就运行不了,这个是部分的代码

classes=[] #创建一个空列表来记录class_dic的内容
total_scorees=[] #存储总成绩字典的列表
c=0
e1=0
e2=0
e3=0
e4=0
e5=0 #e1,2,3,4,5各个等级人数的总和
with open('message.txt',encoding='utf-8')as f: #把一个文件的内容保存到由每一行的字典组成的大列表

for line in f.readlines():
    messagelist=line.strip()       #按行分配
    #print (messagelist) 
    bianhao=messagelist.split("$",3)[0]      #按照空格吧一行的内容分成4份(切3刀),选取第一个元素给编号
        
    score1=messagelist.split("$",3)[1]       #按照空格吧一行的内容分成4份(切3刀),选取第一个元素给编号       
        
    score2=messagelist.split("$",3)[2]
       
    score3=messagelist.split("$",3)[3]
    class_dic={'编号':bianhao,'平时成绩':score1,'期中考试成绩':score2,'期末考试成绩':score3}     #字典来保存一行元素分出来的四个元素


        
    a=[class_dic['平时成绩'],class_dic['期中考试成绩'],class_dic['期末考试成绩']] 
    a=[float(x) for x in a]                 #把字符串变成数字来计算                                 
    b=0.3*a[0]+0.3*a[1]+0.4*a[2] 
    c=c+b          #计算总成绩
    
    if 90<=b <=100:               #划分等级
        dengji='优秀'
        e1=e1+1
    elif 80<=b <=90:
        dengji='良好'
        e2=e2+1
    elif 70<=b <=80:
        dengji='中等'
        e3=e3+1
    elif 60<=b <=70:
        dengji='及格'
        e4=e4+1
    else:
        dengji='不及格'
        e5=e5+1
        

    
    total_score={'编号':bianhao,'总成绩':b,'等级':dengji}                                            #存储每个编号的总成绩        

     
    print(class_dic)
    print('\n')
    classes.append(class_dic)                                    #把字典的信息全部保存到列表中
    total_scorees.append(total_score)                                    #把字典的信息全部保存到列表中
        

for i in range(len(total_scorees)):
print(total_scorees[i]) #输出每个学生的总评平均成绩
d=c/len(total_scorees)
print("班级的总成绩:")
print(d) #输出班级总平均成绩
print("\n")
print("优秀的学生所占人数比:")
e10=e1/len(total_scorees)
print(e10)
print("良好的学生所占人数比:")
e20=e2/len(total_scorees)
print(e20)
print("中等的学生所占人数比:")
e30=e3/len(total_scorees)
print(e30)
print("及格的学生所占人数比:")
e40=e4/len(total_scorees)
print(e40)
print("不及格的学生所占人数比:")
e50=e5/len(total_scorees)
print(e50)
print("\n")
print("优秀的学生如下:")
for total_score in total_scorees:
if total_score['等级'] =='优秀' :
print(total_score['编号'],total_score['总成绩'],total_score['等级'])
print("\n")
print("良好的学生如下:")
for total_score in total_scorees:
if total_score['等级'] =='良好' :
print(total_score['编号'],total_score['总成绩'],total_score['等级'])
print("\n")
print("中等的学生如下:")
for total_score in total_scorees:
if total_score['等级'] =='中等' :
print(total_score['编号'],total_score['总成绩'],total_score['等级'])
print("\n")
print("及格的学生如下:")
for total_score in total_scorees:
if total_score['等级'] =='及格' :
print(total_score['编号'],total_score['总成绩'],total_score['等级'])
print("\n")
print("不及格的学生如下:")
for total_score in total_scorees:
if total_score['等级'] =='不及格' :
print(total_score['编号'],total_score['总成绩'],total_score['等级'])

  • 写回答

3条回答 默认 最新

  • A Python 萌新花花 2022-04-28 19:09
    关注

    这个存粹是看你里里面存的是什么东西了。如果你每一行可以被分割开的东西不超过1个,那么就会报错。
    顺便说一下,是encoding=“utf-8”不是encoding = "utf_8"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月6日
  • 已采纳回答 4月28日
  • 修改了问题 4月28日
  • 创建了问题 4月28日

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c