美文网首页
2019-01-03作业

2019-01-03作业

作者: 百而所思 | 来源:发表于2019-01-03 19:12 被阅读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 = 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'])
    

    相关文章

      网友评论

          本文标题:2019-01-03作业

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