stu={"name":"张三","english":80,"python":90,"math":100} 1)请使用上述结构输入5个学生的name及english、python和math三门课的成绩并存储到列表scores中; 2)计算每个同学的平均成绩,同时在字典中添加关键字"avg"用来表示平均成绩; 3)按照每个学生的平均成绩由高到低排序输出5个学生的相关信息。
时间: 2023-07-02 16:04:07 浏览: 360
1)将5个学生的信息存储到列表scores中:
```
scores = []
stu1 = {"name":"张三","english":80,"python":90,"math":100}
scores.append(stu1)
stu2 = {"name":"李四","english":85,"python":95,"math":90}
scores.append(stu2)
stu3 = {"name":"王五","english":90,"python":80,"math":95}
scores.append(stu3)
stu4 = {"name":"赵六","english":70,"python":75,"math":80}
scores.append(stu4)
stu5 = {"name":"陈七","english":95,"python":85,"math":90}
scores.append(stu5)
```
2)计算每个同学的平均成绩,并向字典中添加"avg"关键字:
```
for stu in scores:
avg_score = (stu["english"] + stu["python"] + stu["math"]) / 3
stu["avg"] = avg_score
```
3)按照每个学生的平均成绩由高到低排序输出5个学生的相关信息:
```
scores_sorted = sorted(scores, key=lambda x: x["avg"], reverse=True)
for stu in scores_sorted:
print("姓名:{},英语成绩:{},Python成绩:{},数学成绩:{},平均成绩:{}".format(stu["name"], stu["english"], stu["python"], stu["math"], stu["avg"]))
```
阅读全文