2019-01-03Day8作业

2019-01-04  本文已影响0人  十二只猴子z

使用一个变量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'}

x = input('姓名:')
y = input('年龄:')
z = input('成绩:')
c = input('电话:')

dict = {'name':x,'age':y,'score':z,'tel':c}

all_students.append(dict)
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'},
] 

message = input('请输入姓名:')
for index in all_students:
    if index['name'] == message:
        print(index)

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'},
] 

num_age = 0
num_score = 0
num = len(all_students)
for student in all_students:
    num_age += student['age']
    num_score += student['score']
print('平均成绩是%.2f,平均年龄是%.2f岁' % (num_score/num,num_age/num))
  1. 删除班级中年龄小于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 x in all_students[:]:
    if x['age'] < 18:
        all_students.remove(x)
print(all_students)
  1. 统计班级中不及格的学生的人数
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 x in all_students[:]:
    if x['score'] < 60:
        num += 1
print("不及格的人数:%d" % num)
  1. 打印手机号最后一位是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 x in all_students:
    if x['tel'][-1] == '2':
        print(x['name'])

上一篇 下一篇

猜你喜欢

热点阅读