Day8 作业

2019-01-03  本文已影响0人  ChiAo1fei
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中

#添加
print('请添加以下信息:')

stu_name = input('姓名:')
stu_age = int(input('年龄:'))
stu_score = int(input('分数:'))
stu_tel = input('电话:')

all_students.append({'name': stu_name, 'age': stu_age, 'score': stu_score, 'tel':stu_tel})

for stu_info in all_students:
    print(stu_info)

运行结果:
请添加以下信息:
姓名:stu5
年龄:22
分数:98
电话:100861
{'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': 'stu5', 'age': 22, 'score': 98, 'tel': '100861'}

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

# 按姓名查询
print('——按姓名查询——')
search_name = input('请输入要查询的名字:')
for stu_info in all_students:
    if search_name == stu_info['name']:
        print(stu_info)
        break
else:
    print('查无此人')

        
运行结果:
——按姓名查询——
请输入要查询的名字:stu1
{'name': 'stu1', 'age': 19, 'score': 81, 'tel': '192222'}

——按姓名查询——
请输入要查询的名字:123
查无此人

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

# 求平均
print('——求平均成绩和平均年龄——')
sum_score = 0
sum_age = 0
for stu_info in all_students:
        sum_score += stu_info['score']
        sum_age += stu_info['age']
print('平均年龄为:',sum_age / len(all_students), '\n平均分数为:',sum_score / len(all_students))

运行结果:
——求平均成绩和平均年龄——
平均年龄为: 22.5 
平均分数为: 70.75

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

print('——删除年龄小于18的学生——')
for stu_info in all_students[:]:
    if stu_info['age'] < 18:
        all_students.remove(stu_info)
for stu_info in all_students:
    print(stu_info)

运行结果:
——删除年龄小于18的学生——
{'name': 'stu1', 'age': 19, 'score': 81, 'tel': '192222'}
{'name': 'stu2', 'age': 29, 'score': 90, 'tel': '211222'}
{'name': 'stu4', 'age': 30, 'score': 45, 'tel': '900012'}

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

# 统计人数
print('——不及格人数——')
score_fail_count = 0
for stu_info in all_students:
    if stu_info['score'] < 60:
        score_fail_count += 1
print('不及格人数:',score_fail_count)

运行结果:
——不及格人数——
不及格人数: 1

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

print('——手机号最后一位是2的学生的姓名——')
for stu_info in all_students:
    if stu_info['tel'][-1] == '2':
        print(stu_info['name'])

运行结果:
——手机号最后一位是2的学生的姓名——
stu1
stu2
stu4
上一篇下一篇

猜你喜欢

热点阅读