day-08 作业

2019-01-03  本文已影响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'}

 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'},
    ] 
name=input('请输入姓名:')
age=int(input('请输入年龄:'))
score=int(input('请输入成绩'))
tel=input('请输入联系方式:')
stu_dict={}
stu_dict['name']=name
stu_dict['age']=age
stu_dict['score']=score
stu_dict['tel']=tel
all_students.append(stu_dict)

2.按姓名查看学生信息:
例如输入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'

print(all_students)
message=input('请输入查找姓名')
for studengt_message in all_students:
    if studengt_message.get('name') == message:
        print(studengt_message)

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_sum=0
for studengt_ages in all_students:
    all_ages_sum+=studengt_ages.get('age')
print('平均年龄为%.1f岁'%(all_ages_sum/len(all_students)))
all_scores_sum=0
for student_score in all_students:
    all_scores_sum+=student_score.get('score')
print('平均分为%.1f分'%(all_scores_sum/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 stu_age in all_students[:]:
    if stu_age.get('age')<18:
        all_students.remove(stu_age)
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 stu_socer in all_students:
    if stu_socer.get('score')<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'},
    ] 
for stu_tel in all_students:
    if stu_tel.get('tel')[-1] == '2':
        print(stu_tel)

上一篇 下一篇

猜你喜欢

热点阅读