这个有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['等级'])