day8 homework

1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话

name = input('请输入姓名')
age = input('请输入年龄')
score = input('请输入成绩')
tel = input('请输入电话号码')
dict_student = {'name':name,'age': age ,'score':score ,'tel':tel }
print(dict_student)

2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)

list_students = []
sum1 = 0
while sum1 < 6:
    name = input('请输入姓名')
    age = input('请输入年龄')
    score = input('请输入成绩')
    tel = input('请输入电话号码')
    dict_student = {'name':name,'age': age ,'score':score ,'tel':tel }
    student = [dict_student]
    list_students += student
    sum1 += 1
print(list_students)

a.统计不及格学生的个数

list_students=[{'name': '赵一', 'age': '23', 'score': '87', 'tel': '13566769808'},
               {'name': '钱二', 'age': '13', 'score': '56', 'tel': '13567890987'},
               {'name': '孙三', 'age': '28', 'score': '90', 'tel': '18478193739'},
               {'name': '李四', 'age': '14', 'score': '48', 'tel': '13878980088'},
               {'name': '周五', 'age': '30', 'score': '76', 'tel': '13765278890'},
               {'name': '吴六', 'age': '18', 'score': '96', 'tel': '13987689323'}]
sum1 = 0
for dict_student in list_students:
    if int(dict_student['score']) < 60:
        sum1+=1
print('不及格人数:',sum1)

b.打印不及格学生的名字和对应的成绩

list_students=[{'name': '赵一', 'age': '23', 'score': '87', 'tel': '13566769808'},
               {'name': '钱二', 'age': '13', 'score': '56', 'tel': '13567890987'},
               {'name': '孙三', 'age': '28', 'score': '90', 'tel': '18478193739'},
               {'name': '李四', 'age': '14', 'score': '48', 'tel': '13878980088'},
               {'name': '周五', 'age': '30', 'score': '76', 'tel': '13765278890'},
               {'name': '吴六', 'age': '18', 'score': '96', 'tel': '13987689323'}]
for dict_student in list_students:
    if int(dict_student['score']) < 60:
        print(dict_student['name'],dict_student['score'])

c.统计未成年学生的个数

list_students=[{'name': '赵一', 'age': '23', 'score': '87', 'tel': '13566769808'},
               {'name': '钱二', 'age': '13', 'score': '56', 'tel': '13567890987'},
               {'name': '孙三', 'age': '28', 'score': '90', 'tel': '18478193739'},
               {'name': '李四', 'age': '14', 'score': '48', 'tel': '13878980088'},
               {'name': '周五', 'age': '30', 'score': '76', 'tel': '13765278890'},
               {'name': '吴六', 'age': '18', 'score': '96', 'tel': '13987689323'}]
sum2 = 0
for dict_student in list_students:
    if int(dict_student['age']) < 18:
        sum2 +=1
print('未成年学生个数:',sum2)

d.打印手机尾号是8的学生的名字

print('手机尾号是8的学生:',end=' ')
list_students=[{'name': '赵一', 'age': '23', 'score': '87', 'tel': '13566769808'},
               {'name': '钱二', 'age': '13', 'score': '56', 'tel': '13567890987'},
               {'name': '孙三', 'age': '28', 'score': '90', 'tel': '18478193739'},
               {'name': '李四', 'age': '14', 'score': '48', 'tel': '13878980088'},
               {'name': '周五', 'age': '30', 'score': '76', 'tel': '13765278890'},
               {'name': '吴六', 'age': '18', 'score': '96', 'tel': '13987689323'}]
for dict_student in list_students:
    tel_num = dict_student['tel']
    if int(tel_num[-1]) == 8:
        print(dict_student['name'],end=' ')

e.打印最高分和对应的学生的名字

list_students=[{'name': '赵一', 'age': '23', 'score': '87', 'tel': '13566769808'},
               {'name': '钱二', 'age': '13', 'score': '56', 'tel': '13567890987'},
               {'name': '孙三', 'age': '28', 'score': '90', 'tel': '18478193739'},
               {'name': '李四', 'age': '14', 'score': '48', 'tel': '13878980088'},
               {'name': '周五', 'age': '30', 'score': '76', 'tel': '13765278890'},
               {'name': '吴六', 'age': '18', 'score': '96', 'tel': '13987689323'}]
score_max = 0
for dict_student in list_students:
    if int(dict_student['score']) > score_max:
        score_max = int(dict_student['score'])
        name = dict_student['name']
print('最高分:',score_max,'学生姓名:',name)

f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

list_students=[{'name': '赵一', 'age': '23', 'score': '87', 'tel': '13566769808'},
               {'name': '钱二', 'age': '13', 'score': '56', 'tel': '13567890987'},
               {'name': '孙三', 'age': '28', 'score': '90', 'tel': '18478193739'},
               {'name': '李四', 'age': '14', 'score': '48', 'tel': '13878980088'},
               {'name': '周五', 'age': '30', 'score': '76', 'tel': '13765278890'},
               {'name': '吴六', 'age': '18', 'score': '96', 'tel': '13987689323'}]
list_students.sort(key =lambda  x: x['score'],reverse=True)
print(list_students)

你可能感兴趣的:(day8 homework)