day8homework

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

student = dict(name='小明', age=18, performance=[{'语文': 89, '数学': 88, '英语': 21}], Tel=13884726472)
print(student)

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

student2 = [
    {'name': '小明', 'age': 18, 'stu_num': 1, 'performance': 88, 'TEL': 13884726472},
    {'name': '小红', 'age': 17, 'stu_num': 2, 'performance': 92, 'TEL': 16593827388},
    {'name': '小白', 'age': 19, 'stu_num': 3, 'performance': 55, 'TEL': 17464528846},
    {'name': '小蓝', 'age': 17, 'stu_num': 4, 'performance': 98, 'TEL': 12567383278},
    {'name': '小绿', 'age': 20, 'stu_num': 5, 'performance': 95, 'TEL': 19284747374},
    {'name': '小黄', 'age': 18, 'stu_num': 6, 'performance': 20, 'TEL': 16888378222}

]

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

population = 0
for stu in student2:
    if stu['performance'] <= 60:
        population += 1
print('不合格人数有%d个' % population)

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

for stu in student2:
    if stu['performance'] < 60:
        print(stu['name'],stu['performance'])

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

time = 0
for stu in student2:
    if stu['age'] < 18:
        time += 1
print('未成年的学生有%d个' % time)

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

for stu in student2:
    if str(stu['TEL'])[-1] == '8':
        print(stu['name'])

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

num1 = 0
names = ''
for stu in student2:
    if stu['performance'] > num1:
        num1 = stu['performance']
        names = stu['name']
print(names)

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

# 方法1
list(sorted(student2, key=lambda x: x['performance'], reverse=True))
======================
performance1 = stundent2[0]['performance']
for index in range(1,len(student2)):
    for stu in student2[index]:
        if student2[index]['performance'] > performance1:
==========================
# 方法2
list1 = []
list2 = []
for stu in student2:
    list1.append(stu['performance'])
list1.sort(reverse= True )
for performance1 in list1:
    for stu in student2:
        if performance1 == stu['performance']:
            list2.append(stu)
print(list2)

尝试实现学生管理系统的界面(见视频)

print('''
===========================================================
    1:添加学生
    2:查看学生
    3:修改学生信息
    4:删除学生
    5:退出
===========================================================
''')
sub = input('请在(1-5)中选择:')
while sub == '1':
    print('请输入学生姓名:/n','请输入学生年龄/n','请输入学生学号:')
    print('添加成功')

你可能感兴趣的:(day8homework)