Try:
with open("class_score.txt","r")as file:
lines=file.readlines()
for item in lines:
name,math,chinese=item.split("\t")
stu={}
stu["name"]=name
stu["math"]=float(math)
stu["chinese"]=float(chinese)
stulist.append(stu)
except:
print("读取数据文件错误")
def printstu(stulist):
print("姓名\t数学\t语文\t")
for stin stulist:
print("{}\t{}\t{}\t".format(stu["name"],stu["math"],stu["chinese"]))
def getNotPass(stulist):
print("以下同学两门课程均不及格:")
for stin stulist:
if stu["math"]<60 and stu["chinese"]<60:
print("姓名:{}数学:{}语文:{}".format(stu["name"],stu["math"],stu["chinese"]))
def getGood(stulist):
print("以下同学两门课程均分大于90:")
for stin stulist:
if(stu["math"]+stu["chinese"])/2>90:
print("姓名:{}数学:{}语文:{}".format(stu["name"],stu["math"],stu["chinese"]))
def getaverage(stulist):
summath=sumchinese=0
for stin stulist:
summath+=stu["math"]
sumchinese+=stu["chinese"]
avgmath=summath/len(stulist)
avgchinese=sumchinese/len(stulist)
print("本班级的数学平均分是{},语文平均分是{}"\
.format(avgmath,avgchinese))
stulist=[]
readstu(stulist)
printstu(stulist)
getaverage(stulist)
getNotPass(stulist)