2019-01-03作业

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

name = input('请输入要添加的名字:')
age = input('请输入要添加的年龄:')
score = input('请输入要添加的成绩:')
tel = input('请输入要天剑的电话:')
all_students.append({'name':name, 'age': age, 'score': score, 'tel': tel})
print(all_students[-1])
all_students.append({'name':1})
print('请输入需要添加的信息')
n = input('名字:')
all_students[-1]['name'] = n
n = input('年龄:')
all_students[-1]['age'] = n
n = input('成绩:')
all_students[-1]['score'] = n
n = input('电话:')
all_students[-1]['tel'] = n
print(all_students[-1])

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

n = input('输入名字,查看相关信息:')
for x in all_students:
    if n == x['name']:
        print(x)

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'}]
sum_age = 0
sum_score = 0
i = 0
for x in all_students:
    sum_age += x['age']
    sum_score += x['score']
    i += 1
print('平均成绩:%.2f,平均年龄:%.2f' % (sum_score/i, sum_age/i))

4.删除班级中年龄小于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)

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

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'}]
i = 0
for x in all_students[:]:
    if x['score'] < 60:
        i += 1
print(i)

6.打印手机号最后一位是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'])
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...
    LittleBear_6c91阅读 218评论 0 1
  • 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...
    woming阅读 159评论 0 0
  • 8月22日-----字符串相关 2-3 个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消...
    future_d180阅读 999评论 0 1
  • 一、面向对象编程编程思想:1.面向过程编程 ---> 算法,逻辑(数学逻辑)2.函数式编程 ---> 函数,模块3...
    Smr_T阅读 533评论 0 0
  • 偶尔,总想起曾经的多少事,那么多的事,亦如碎屑随风飘舞,遗存的唯有那静静天宇,与那不再孤单的阵阵清影。 人生有多少...
    6521d5bb6dd6阅读 226评论 0 0