Day8 作业

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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...
    woming阅读 1,297评论 0 0
  • 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...
    圣咸鱼阅读 1,400评论 0 0
  • 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...
    xue_y阅读 1,382评论 0 0
  • (学生管理系统简易版)用一个变量来保存一个班级的学生信息,学生信息包括:姓名、学号、成绩(英语、体育、美术、数学)...
    我才是鳄鱼宝宝阅读 1,441评论 0 0
  • 2016年3月27日 北京 极客邦科技 (InfoQ)主题:开源架构演进、IT技术社区生态、社群运营(商业化&N...
    RiboseYim阅读 5,462评论 0 0

友情链接更多精彩内容