使用一个变量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'}
"""
stu_name = input('输入学生name: ')
stu_age = int(input('输入学生age: '))
stu_score = int(input('输入学生score: '))
stu_tel = int(input('输入学生tel: '))
add_stu = {'name': stu_name, 'age': stu_age, 'score': stu_score, 'tel': stu_tel}
all_students.append(add_stu)
for item in all_students:
print(item)
2.按姓名查看学生信息:
"""
例如输入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'
"""
l_name = input('输入name: ')
for stu_massage in all_students:
if stu_massage['name'] == l_name:
print(stu_massage)
break
else:
print('没有该学生信息!')
3.求所有学生的平均成绩和平均年龄
list_score = []
list_age = []
for stu_massage in all_students:
list_score.append(stu_massage['score'])
list_age.append(stu_massage['age'])
average_score = sum(list_score) / len(all_students)
average_age = sum(list_age) / len(all_students)
print('平均年龄:{},平均成绩:{}'.format(average_age, average_score))
4.删除班级中年龄小于18岁的学生
#1.
all_students1 = all_students[:]
all_students.clear()
for stu_massage in all_students1:
if stu_massage['age'] >= 18:
all_students.append(stu_massage)
print(all_students)
#2.
for stu_massage in all_students[:]:
if stu_massage['age'] < 18:
all_students.remove(stu_massage)
print(all_students)
5.统计班级中不及格的学生的人数
num = 0
for stu_massage in all_students:
if stu_massage['score'] < 60:
num += 1
print('有%d人不及格'%num)
6.打印手机号最后一位是2的学生的姓名
for stu_massage in all_students:
if int(stu_massage['tel'][-1]) == 2:
print(stu_massage['name'])
网友评论