day 006作业 7-23

2018-07-23  本文已影响0人  Yehao_

程序功能:

# 定义变量classmates保存学生信息
classmates = [
    {'name': '张三', 'student_id': 1, 'score': {'English': 95, 'PE': 98, 'Paint': 88, 'Math': 97}},
    {'name': '李四', 'student_id': 2, 'score': {'English': 66, 'PE': 86, 'Paint': 68, 'Math': 76}},
    {'name': '王五', 'student_id': 3, 'score': {'English': 45, 'PE': 58, 'Paint': 59, 'Math': 99}},
]


# 添加学生
classmates.append(  {'name': '赵四', 'student_id': 4, 'score': {'English': 95, 'PE': 98, 'Paint': 88, 'Math': 97}})
print(classmates)

# 根据姓名查看班级里某个学生的信息
for member in classmates:
    for v in member.values():
        if v == '王五':
            index = classmates.index(member)
            print(classmates[index])  # 根据姓名查看某个学生的信息
            # 求某个学生的平均成绩
            scores_sum = 0  
            score = classmates[index].get('score')
            english_score = score['English']
            PE_score = score['PE']
            paint_score = score['Paint']
            math_score = score['Math']
            # print(score)
            scores_sum = english_score + PE_score + paint_score + math_score
            avg_score = scores_sum / 4
            print(avg_score)  # 输出某个学生的平均成绩
            classmates.pop(index)  # 根据姓名删除指定学生
            print(classmates)  # 删除指定学生后的班级所有学生信息

Output:
[{'name': '张三', 'student_id': 1, 'score': {'English': 95, 'PE': 98, 'Paint': 88, 'Math': 97}}, {'name': '李四', 'student_id': 2, 'score': {'English': 66, 'PE': 86, 'Paint': 68, 'Math': 76}}, {'name': '王五', 'student_id': 3, 'score': {'English': 45, 'PE': 58, 'Paint': 59, 'Math': 99}}, {'name': '赵四', 'student_id': 4, 'score': {'English': 95, 'PE': 98, 'Paint': 88, 'Math': 97}}]
{'name': '王五', 'student_id': 3, 'score': {'English': 45, 'PE': 58, 'Paint': 59, 'Math': 99}}
65.25
[{'name': '张三', 'student_id': 1, 'score': {'English': 95, 'PE': 98, 'Paint': 88, 'Math': 97}}, {'name': '李四', 'student_id': 2, 'score': {'English': 66, 'PE': 86, 'Paint': 68, 'Math': 76}}, {'name': '赵四', 'student_id': 4, 'score': {'English': 95, 'PE': 98, 'Paint': 88, 'Math': 97}}]
上一篇下一篇

猜你喜欢

热点阅读