美文网首页
day8作业

day8作业

作者: xue_y | 来源:发表于2019-01-03 17:57 被阅读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_student = [
{'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'}
]
student = {}
add_name = input('姓名:')
add_age =int(input('年龄:'))
add_score = float(input('成绩:'))
add_tel = input('电话:')
student['name'] = add_name
student['age'] = add_age
student['score'] = add_score
student['tel'] = add_tel
all_student.append(student)
print(all_student)

2.按姓名查看学生信息:

例如输入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'
find_name = input('请输入查找学生名字:')
for item in all_student:
    if find_name == item['name']:
        print(item)
        break
else:
    print('没有该学员')

3.求所有学生的平均成绩和平均年龄

a = 0
b = 0
for item in all_student:
    sum_age = item['age'] + a
    a = sum_age
    sum_score = item['score'] + b
    b = sum_score
    average_age = sum_age / len(all_student)
    average_score = sum_score / len(all_student)
print('学生的平均年龄为%d,平均成绩为%d' % (average_age, average_score))

4.删除班级中年龄小于18岁的学生

for item in all_student[:]:
    if item['age'] < 18:
        all_student.remove(item)
print(all_student)

5.统计班级中不及格的学生的人数

num1 = 0
for item in all_student[:]:
    if int(item['score']) < 60:
        num1 += 1
print(num1)

6.打印手机号最后一位是2的学生的姓名

for item in all_student[:]:
    if int(item['tel']) % 10 == 2:
        print(item['name'])

相关文章

网友评论

      本文标题:day8作业

      本文链接:https://www.haomeiwen.com/subject/lehqrqtx.html