美文网首页
day-08 作业

day-08 作业

作者: 哗啦噼里啪啦 | 来源:发表于2019-01-03 17:28 被阅读0次

使用一个变量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=int(input('请输入年龄:'))
score=int(input('请输入成绩'))
tel=input('请输入联系方式:')
stu_dict={}
stu_dict['name']=name
stu_dict['age']=age
stu_dict['score']=score
stu_dict['tel']=tel
all_students.append(stu_dict)

2.按姓名查看学生信息:
例如输入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'

print(all_students)
message=input('请输入查找姓名')
for studengt_message in all_students:
    if studengt_message.get('name') == message:
        print(studengt_message)

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'},
    ] 
all_ages_sum=0
for studengt_ages in all_students:
    all_ages_sum+=studengt_ages.get('age')
print('平均年龄为%.1f岁'%(all_ages_sum/len(all_students)))
all_scores_sum=0
for student_score in all_students:
    all_scores_sum+=student_score.get('score')
print('平均分为%.1f分'%(all_scores_sum/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'},
    ] 
for stu_age in all_students[:]:
    if stu_age.get('age')<18:
        all_students.remove(stu_age)
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'},
    ] 
num=0
for stu_socer in all_students:
    if stu_socer.get('score')<60:
        num+=1
print('班上不及格的人有%d位'%num)

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 stu_tel in all_students:
    if stu_tel.get('tel')[-1] == '2':
        print(stu_tel)

相关文章

  • day-08 作业

    编写⼀个函数求1+2+3+...+N的和 2.编写⼀个函数求多个数中的最⼤值 编写⼀个函数,实现摇⾊⼦的功能,打印...

  • day-08 作业

    使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...

  • Day-08

  • Day-08 练习

    使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话 1.添...

  • day-08 总结

    容器性数据 1.元祖(tuple) 1.什么是元祖(tuple) python提供的容器型数据类型,不可变并且有序...

  • 课程学习-第五章/第六章

    字符串连接运算符 接收用户键盘输入 String ss = sc.next(); 第六章:控制语句 day-08课...

  • 日更40/100:不够强大,是我的错。

    不够强大,是我的错。 ——精听《刘润·商业洞察力30讲》&精读《系统之美》DAY-08 《刘润·商业洞察力30讲》...

  • [Python] (Day-08) - 元组

    Python 的元组与列表类似,不同之处在于元组的元素不能修改 元组使用小括号,列表使用方括号 元组创建很简单,只...

  • Day-08 容器类型

    1.什么是元祖(tuple)python提供的容器型数据类型,不可变并且有序。(元祖就是不可变的列表)不可变 - ...

  • day-08 函数基础 总结

    字典相关方法 1.clear字典.clear()------清空字典内所有键值对,删除所有元素 2.copy字典....

网友评论

      本文标题:day-08 作业

      本文链接:https://www.haomeiwen.com/subject/tdlqrqtx.html