2019-01-03

使用一个变量),每个学生需要保存:姓名、年龄、成绩、电话

all_students = [
    {'name':'小明', 'age': 19, 'score':81, 'tel':'192222'},
    {'name':'小张', 'age': 29, 'score':90, 'tel':'211222'},
    {'name':'小李', 'age': 12, 'score':67, 'tel':'521114'},
    {'name':'小雨', 'age': 30, 'score':45, 'tel':'900012'},
] 

1.添加学生:输入学生信息,将输入的学生的信息保存到all_students中

例如输入:
姓名: 小明
年龄: 20
成绩: 100
电话: 111922
那么就在all_students中添加{'name':'小明', 'age': 20,
'score': 100, 'tel':'111922'}

while True:
        name = input('请输入学生姓名:')
        age = int(input('请输入学生年龄:'))
        score = int(input('请输入学生成绩:'))
        tel = int(input('请输入学生电话:'))
        print('添加成功')
        all_students.append({'name':name,'age':age,'score': score,'tel':tel})
        choice_1 = int(input('1.继续\n2.返回'))
        if choice_1 == 2:
            break
    print(all_students)

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

例如输入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81,
'tel':'192222'

all_students = [
{'name': '小明', 'age': 19, 'score': 81, 'tel': '192222'},
{'name': '小红', 'age': 29, 'score': 90, 'tel': '211222'},
{'name': '小张', 'age': 12, 'score': 67, 'tel': '521114'},
{'name': '小雨', 'age': 30, 'score': 45, 'tel': '900012'},
      ]
name_message = input('请输入要查看的学生的姓名:')
for student in all_students:
if student['name'] == name_message:
    print(student)
    break
else:
print('该学生不存在')

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

all_students = [
    {'name': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
    {'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
    {'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
    {'name': '小红', 'age': 30, 'score': 45, 'tel': '900012'},
]
sum_scores = 0
sum_ages = 0
for student in all_students:
    sum_scores += student['score']
    sum_ages += student['age']
print('所有学生的平均成绩为%.2f' % (sum_scores / len(all_students)))
print('所有学生的平均年龄为%.2f' % (sum_ages / len(all_students)))

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

all_students = [
    {'name': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
    {'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
    {'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
    {'name': '小红', 'age': 30, 'score': 45, 'tel': '900012'},
]
index = 0
while index < len(all_students):
    if all_students[index]['age'] < 18:
        del all_students[index]
    else:
        index += 1
print(all_students)

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

all_students = [
    {'name': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
    {'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
    {'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
    {'name': '小红', 'age': 30, 'score': 45, 'tel': '900012'},
]
count = 0
for index in range(len(all_students)):
    if all_students[index]['score'] < 60:
        count += 1
print('班级中不及格的学生的人数为%d' % count)

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

all_students = [
    {'name': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
    {'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
    {'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
    {'name': '小红', 'age': 30, 'score': 45, 'tel': '900012'},
]
for student in all_students:
    if student['tel'][len(student['tel']) - 1] == '2':
        print(student['name'])

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

相关阅读更多精彩内容

  • 作业# 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话...
    多多爸是小白阅读 245评论 0赞 0
  • 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...
    woming阅读 173评论 0赞 0
  • 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...
    百而所思阅读 103评论 0赞 0
  • 1.什么是元祖(tuple)python提供的容器型数据类型,不可变并且有序。(元祖就是不可变的列表)不可变 - ...
    多多爸是小白阅读 397评论 0赞 0
  • 上图就是最终的效果图,是不是觉得很酷炫。其实很简单,只需要手机和最入门级的PhotoShop操作就能完成了。 岔开...
    findingsea阅读 2,070评论 9赞 46

友情链接更多精彩内容