1.添加学生:输入学生信息,将输入的学生的信息保存到all_students中
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 = input('年龄:')
new_score = input('成绩:')
new_tel = input('电话:')
new_student = {'name':new_name, 'age':new_age ,'score':new_score, 'tel':new_tel}
all_students.append(new_student)
print(all_students)
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('姓名:')
for studet in all_students:
if studet['name'] == new_name:
print(studet)
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'},]
sum_score = 0
sum_age =0
for student in all_students:
sum_score += student['score']
sum_age += student['age']
print('平均成绩:',sum_score / len(all_students) , '平均年龄:',sum_age / 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'},]
index = 0
for student in all_students:
index += 1
if student['age'] < 18:
del all_students[index-1]
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'},]
index = 0
for student in all_students:
if student['score'] < 60:
index += 1
print(index)
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'},]
index = 0
for student in all_students:
if student['tel'][-1] == '2':
print(student['name'])