2019-01-03 day8 作业

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

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'},]
new_name = input('姓名:')
new_age = input('年龄:')
new_score = input('成绩:')
new_tel = input('电话:')
new_student = {'name':new_name, 'age':new_age ,'score':new_score, 'tel':new_tel}
all_students.append(new_student)
print(all_students)

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'},]
new_name = input('姓名:')
for studet in all_students:
    if studet['name'] == new_name:
        print(studet)

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_score = 0
    sum_age =0
    for student in all_students:
        sum_score += student['score']
        sum_age += student['age']
    print('平均成绩:',sum_score / len(all_students) , '平均年龄:',sum_age / len(all_students))

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'},]
index = 0
for student in all_students:
    index += 1
    if student['age'] < 18:
        del all_students[index-1]
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'},]
index = 0
for student in all_students:
    if student['score'] < 60:
        index += 1
print(index)

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

相关阅读更多精彩内容

  • 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...
    woming阅读 1,297评论 0 0
  • 作业# 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话...
    多多爸是小白阅读 1,387评论 0 0
  • 使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...
    LittleBear_6c91阅读 1,576评论 0 1
  • 文/水木清 近年来,米叔的电影火爆,《神秘巨星》创下七点四亿的好成绩,而《摔跤吧!爸爸》更是创下了12.93的票房...
    刹那年华之水木清阅读 2,299评论 2 7
  • 曾经的冥思苦想 被感悟 凝结成绚烂的真谛 一旦意识到荒谬 便已经被鲜血和泪灌满 所谓的成熟 或许会被仰望的星空 铺...
    冷冬年阅读 1,387评论 0 1

友情链接更多精彩内容