01-03

作者: ychaochaochao | 来源:发表于2019-02-16 17:50 被阅读0次

使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话

all_students = [
    {'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
    {'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
    {'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
    {'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
] 

1.添加学生:输入学生信息,将输入的学生的信息保存到all_students中

例如输入:
姓名: 小明
年龄: 20
成绩: 100
电话: 111922  
那么就在all_students中添加{'name':'小明', 'age': 20, 'score': 100, 'tel':'111922'}

2.按姓名查看学生信息:

例如输入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'

3.求所有学生的平均成绩和平均年龄

4.删除班级中年龄小于18岁的学生

5.统计班级中不及格的学生的人数

6.打印手机号最后一位是2的学生的姓名

all_students = [
    {'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
    {'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
    {'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
    {'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
    ]
new_name=input('姓名:')
new_age=int(input('年龄:'))
new_score=int(input('成绩:'))
new_tel=input('电话:')
#将输入的学生信息存在一个新的字典new_stu中
new_stu={'name':new_name, 'age':new_age , 'score':new_score, 'tel':new_tel}
#将new_stu添加到原列表all_student中
all_students.append(new_stu)
print(all_students)

check_name=input('请输入要查找的学生姓名:')
for index in range(len(all_students)):
    # for key in all_students[index]:
    #     if check_name ==key:
    #       print(all_students[index])

#2.按姓名查看学生信息:
    if check_name == all_students[index].get('name'):
        print(all_students[index])


# 3.求所有学生的平均成绩和平均年龄
ave_scores = []
ave_age = []
for  index  in  range(len(all_students)):
    ave_age.append(all_students[index].get('age'))
    ave_scores.append(all_students[index].get('score'))
print('平均成绩:',sum(ave_scores)/len(ave_scores))
print('平均年龄:',sum(ave_age)/len(ave_age))



# 5.统计班级中不及格的学生的人数
num = 0  #用来存储不及格人数
for  index  in  range(len(all_students)):
    if all_students[index].get('score') < 60:
        num += 1
print('不及格的人有:',num)

# 6.打印手机号最后一位是2的学生的姓名
print('手机号最后一位是2的学生有:')
for  index  in  range(len(all_students)):
   if  '2'== all_students[index].get('tel')[-1]:
       print(all_students[index].get('name'))

# 4.删除班级中年龄小于18岁的学生
index = 0
while index < len(all_students):
    age = all_students[index].get('age')
    if age < 18:
        del all_students[index]
    else:
        index += 1
print(all_students)

相关文章

  • 01-03

    /文章/ UI产品设计14点心得 UI设计师的个人创作流程,灵感搜集与创作规范。 产品设计:没有完美的方案,只有更...

  • 01-03

    使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...

  • 01-03

  • 01-03失去

    失去,只是宇宙对你说: “现在这些再也不适合你了,所以我要替你清出一些空间,让新的,对的,正确的,更好的,更适合你...

  • 01-03 随笔

    工作: 协助组长进行了一次数据保存验证,耗时半小时,完成。 生活: 王者荣耀段位在钻石1,还差3颗星就能够到星耀,...

  • 01-03小记

    有一丝不真切的感觉 是与他人竞争后的空虚,有点怀念和大家一起学习的生活 感觉一下子失去了学习的欲望 其实进入到学习...

  • 2021.10.12

    03:01-03:14 06:32-06:50 09:42-10:00 12:23-12:32 15:04-15:...

  • Ambari 2.6.2安装Hadoop hbase集群

    一、环境准备 1、集群规划 01和02主要做管理机,03-05做数据数据节点,01-03做zookeeper集群。...

  • ES6

    目录 01-01 Let、const命令 01-02 解构解析 01-03 正则扩展 01-04 字符串扩展 01...

  • 核心课程第4课 #咨询师#

    前沿:咨询师的本质:帮助对方从现状---到达理想状态 01-03 课程内容 04 践行 01找到标签 兴趣:时间,...

网友评论

      本文标题:01-03

      本文链接:https://www.haomeiwen.com/subject/qxyqrqtx.html