Day8作业

使用一个变量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'}
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'},
]
n1 = input('请输入学生姓名:')
n2 = input('请输入学生年龄:')
n3 = input('请输入学生成绩:')
n4 = input('请输入学生电话:')
dict1 = {'name': n1,  'age': n2, 'score': n3, 'tel': n4}
all_students.append(dict1)
print(all_students)

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

例如输入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'
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'},
]
n1 = input('请输入学生姓名:')
for dict1 in all_students:
    for key in dict1:
        if dict1[key] == n1:
            print(dict1)

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

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'},
]
sum1 = 0
sum2 = 0
for dict1 in all_students:
    for key in dict1:
        if key == 'score':
            sum1 += dict1[key]
        elif key == 'age':
            sum2 += dict1[key]
print('平均成绩为:%d' % (sum1 // len(all_students)))
print('平均年龄为:%d' % (sum2 // len(all_students)))

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

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'},
]
for dict1 in all_students[:]:
    for key in dict1:
        if key == 'age' and dict1[key] < 18:
            all_students.remove(dict1)
print(all_students)

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

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'},
]
num =0
for dict1 in all_students:
    for key in dict1:
        if key == 'score' and dict1[key] < 60:
            num += 1
print('不及格的人数为:%d人' % num)

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'},
]
names = []
for dict in all_students:
    sum = int(dict['tel'])
    if sum % 10 == 2:
         name = dict['name']
         names.append(name)
print(names)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • (学生管理系统简易版)用一个变量来保存一个班级的学生信息,学生信息包括:姓名、学号、成绩(英语、体育、美术、数学)...
    我才是鳄鱼宝宝阅读 1,441评论 0 0
  • 1.写一个程序 a.用一个变量来保存一个班级的学生信息(姓名,学号,成绩(英语,美术,体育,数学),年龄)b.给这...
    RurouniKenshin阅读 2,394评论 0 12
  • (学生管理系统简易版)用一个变量来保存一个班级的学生信息,学生信息包括:姓名、学号、成绩(英语、体育、美术、数学)...
    逆流而上_2eb6阅读 2,656评论 0 0
  • (学生管理系统简易版)用一个变量来保存一个班级的学生信息,学生信息包括:姓名、学号、成绩(英语、体育、美术、数学)...
    雨雨雨90阅读 4,007评论 0 2
  • 每周一本书,进步不止一点点。这里是椰子私塾。 今天我们主要了解,政府在调控供求、房租管制、制订最低工资法方面,从经...
    丨张伟丨阅读 1,828评论 0 1

友情链接更多精彩内容