day 006作业 7-23

程序功能:

  • 用一个变量来保存一个班级的学生信息
  • 给这个班级添加学生
  • 根据姓名查看班级里的某个学生的信息
  • 根据姓名删除一个指定的学生信息
    *查看班级的所有的学生信息
    *求指定的学生平均成绩
# 定义变量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}}]
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • vertical-align vertical-align 用来指定行内元素(inline)或表格单元格(tabl...
    sunny519111阅读 1,612评论 0 0
  • 自从来了学校,我才知道之前所有我想的一切都爆炸了。我没有忘记喜欢他的感觉,自然也想要靠近他,前两天看到了一个帖子,...
    阿四兔阅读 1,618评论 0 0
  • Unite5.1-1
    Justina_Fu阅读 1,111评论 0 0
  • 最近最火也最具争议的电影应该算是5月6日上映的《百鸟朝凤》了吧。 不过,一部电影,承载了导演的离世与制片人的下跪,...
    安薇阅读 4,260评论 0 2