1.有如下学生的成绩信息: s1 = {"姓名":"乔峰","班级":"1班","数学":88,"语文":87,"英语":90} s2 = {"姓名":"段誉","班级":"2班","数学":98,"语文":77,"英语":95} s3 = {"姓名":"阿朱","班级":"1班","数学":78,"语文":83,"英语":80} s4 = {"姓名":"阿紫","班级":"1班","数学":75,"语文":80,"英语":86} s5 = {"姓名":"虚竹","班级":"2班","数学":93,"语文":85,"英语":96} 完成以下操作:(1)遍历输出1班学生的成绩单(2)求出每个学生的总成绩后加到原有字典中然后打印每个学生信息(3)求出每门课的平均成绩并输出(4)找出数学低于平均分的学生,打印他们的姓名,班级,数学成绩。效果如图:
代码如下:
s1 = {"姓名":"乔峰","班级":"1班","数学":88,"语文":87,"英语":90}
s2 = {"姓名":"段誉","班级":"2班","数学":98,"语文":77,"英语":95}
s3 = {"姓名":"阿朱","班级":"1班","数学":78,"语文":83,"英语":80}
s4 = {"姓名":"阿紫","班级":"1班","数学":75,"语文":80,"英语":86}
s5 = {"姓名":"虚竹","班级":"2班","数学":93,"语文":85,"英语":96}
tb=[s1,s2,s3,s4,s5]
print("1班学生成绩单:")
for k in s1.keys():
print(k,end=' ')
print()
for x in range(len(tb)):
if tb[x].get("班级")=="1班":
print(tb[x].get("姓名")," ",tb[x].get("班级")," ",tb[x].get("数学")," ",tb[x].get("语文")," ",tb[x].get("英语"))
print("加入个人总分后:")
s1["总分"]=0
for k in s1.keys():
print(k,end=' ')
print()
for i in range(len(tb)):
a,b,c=tb[i].get("数学"),tb[i].get("语文"),tb[i].get("英语")
aa={'总分':(a+b+c)}
tb[i].update(aa)
print(tb[i].get("姓名")," ",tb[i].get("班级")," ",tb[i].get("数学")," ",tb[i].get("语文")," ",tb[i].get("英语")," ",tb[i].get("总分"))
sum1,sum2,sum3=0,0,0
for i in tb:
for k,v in i.items():#求年龄平均值
if k=="数学":
sum1=sum1+v
if k=="语文":
sum2=sum2+v
if k=="英语":
sum3=sum3+v
print("数学平均分:",sum1/len(tb))
print("语文平均分:",sum2/len(tb))
print("英语平均分:",sum3/len(tb))
t=sum1/len(tb)
print("数学成绩低于平均分的学生:")
for j in range(len(tb)):
if tb[j].get("数学")<t:
print(tb[j].get("姓名"),tb[j].get("班级"),tb[j].get("数学"))
运行结果:
文章来源:https://www.toymoban.com/news/detail-442083.html
文章来源地址https://www.toymoban.com/news/detail-442083.html
到了这里,关于Python字典的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!