美文网首页
Day08-作业

Day08-作业

作者: 萌王史莱姆 | 来源:发表于2019-01-03 19:09 被阅读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': '小花', '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'},
    ]
    student = {}
    name = input('请输入学生的姓名:')
    student['name'] = name
    age = int(input('请输入学生的年龄:'))
    student['age'] = age
    score = float(input('请输入学生的成绩:'))
    student['score'] = score
    tel = input('请输入学生的电话')
    student['tel'] = tel
    all_students.append(student)
    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'])
    

    相关文章

      网友评论

          本文标题:Day08-作业

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