求各位大神指导,刚学点Python的基础
已知字典:
stu1 = {'name':'张三','score':80}
stu2 = {'name':'李四','score':90}
stu3 = {'name':'王五','score':85}
输出平均成绩及最高分同学
sum([stu1['score'],stu2['score'],stu3['score']])/3
sorted([stu1, stu2, stu3], key=lambda x: x['score'], reverse=True)[0]['name']
1.取出每个字典中的第二个value值
2.循环比较大小
3.输出平均成绩和最高分
students = [
{'name':'张三','score':80},
{'name':'李四','score':90},
{'name':'王五','score':85}
]
# calculate average score
total_score = 0
for student in students:
total_score += student['score']
average_score = total_score / len(students)
# find the student with the highest score
max_score = 0
max_score_student = None
for student in students:
if student['score'] > max_score:
max_score = student['score']
max_score_student = student
# print the results
print("平均分:", average_score)
print("最高分同学:", max_score_student['name'], max_score_student['score'])