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 = []
# dict1 = {}
# # shuru = input('是否输入:')
# while True:
#     shuru = input('是否输入(1.继续):')
#     if shuru == '1':
#         name = input('请输入姓名:')
#         age = input('请输入年龄:')
#         score = input('请输入成绩:')
#         tel = input('请输入电话:')
#         dict1 = {'name': name, 'age': age, 'score': score, 'tel': tel}
#         all_students.append(dict1)
#     else:
#         break
#
# print(all_students)
"""
[{'name': 'h', 'age': '20', 'score': '98', 'tel': '123'},
{'name': 'hf', 'age': '20', 'score': '88', 'tel': '234'}]
"""

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'},
# ]
# name1 = input('请输入要查找的姓名:')
# index = 0
# for dict1 in all_students[:]:
#     index += 1
#     if dict1['name'] == name1:
#         print(index, dict1)
"""
请输入要查找的姓名:stu4
4 {'name': 'stu4', 'age': 30, 'score': 45, 'tel': '900012'}
"""

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'},
]
all_ages = 0
all_scores = 0
for item in all_students:
    all_ages += item['age']
    all_scores += item['score']
print('平均成绩:%.2f平均年龄:%d' % (all_scores / len(str(all_scores)), all_ages / len(str(all_ages))))
# 平均成绩:94.33平均年龄:45

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 item in all_students[:]:
    if item['age']<18:
        all_students.remove(item)
print(all_students)
"""
[{'name': 'stu1', 'age': 19, 'score': 81, 'tel': '192222'},
 {'name': 'stu2', 'age': 29, 'score': 90, 'tel': '211222'}, 
 {'name': 'stu4', 'age': 30, 'score': 45, 'tel': '900012'}]
"""

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'},
# ]
# count = 0
# for x in all_students:
#     if x['score']<60:
#         count+=1
# print(count)  # 1

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'},
]
for dict1 in all_students:
    if dict1['tel'][-1] == '2':
        print(dict1['name'], end=' ')  # stu1 stu2 stu4
```

你可能感兴趣的:(day8 作业)