day6作业

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

import random
name = ['明', '红', '黄', '蓝', '绿', '饼干', '邋遢', '花']
sexes = ['男', '女', '不明']
students = []
for i in range(6):
    student = {
        'name': '小' + name[random.randint(0, 7)],
        'age': random.randint(15, 21),
        'score': random.randint(50, 100),
        'tel': '1234567891' + str(random.randint(0, 9)),
        'sex': sexes[random.randint(0, 2)]
    }
    students.append(student)

2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
a.统计不及格学生的个数


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

num = 0
for i in students:
    if i['score'] < 60:
        num += 1
        print('不及格的人:', i['name'], '分数是:', i['score'])
print('不及格人数:', num)

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

young_man = 0
for age in students:
    if age['age'] < 18:
        young_man += 1
print('未成年人数:', young_man)

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

for tel in students:
    if int(tel['tel'][-1]) == 8:
        print('尾号是8的学生:', tel['name'])

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

max_score = 0
max_name = []
for score in students:
    if score['score'] > max_score:
        max_score = score['score']
        # max_name = score['name']
for name in students:
    if name['score'] == max_score:
        max_name.append(name)
for num_people in max_name:
    print('学生:', num_people['name'], '分数:', max_score, '最高')

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

for x in range(len(students)-1):
    for y in range(x, len(students)):
        if students[x]['score'] < students[y]['score']:
            students[x], students[y] = students[y], students[x]
print(students)

​ g.删除性别不明的所有学生

for sex in students.copy():
    if sex['sex'] == '不明':
        students.remove(sex)
print(students)

3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人

name = ['明', '红', '黄', '蓝', '绿', '饼干', '邋遢', '花']
course1 = []
course2 = []
course3 = []
for i in range(5):
    name1 = '小' + name[random.randint(0, 7)]
    if name1 not in course1:
        course1.append(name1)
for i in range(5):
    name1 = '小' + name[random.randint(0, 7)]
    if name1 not in course2:
        course2.append(name1)
for i in range(5):
    name1 = '小' + name[random.randint(0, 7)]
    if name1 not in course3:
        course3.append(name1)
print(course1)
print(course2)
print(course3)
print('======a=======')
course = course1+course2+course3
key_course = dict.fromkeys(course)
print(list(key_course), len(list(key_course)))
b. 求只选了第一个学科的人的数量和对应的名字
num1_stu = 0
for i in course1:
    if i not in course2 and i not in course3:
        print('只选第一个学科人:', i)
        num1_stu += 1
print(num1_stu)

​ c. 求只选了一门学科的学生的数量和对应的名字

num2 = 0
for num1_people in course:
    if course.count(num1_people) == 1:
        print('只选了一门课程的人', num1_people)
        num2 += 1
print(num2)
d. 求只选了两门学科的学生的数量和对应的名字
num3 = []
for num2_people in course:
    if course.count(num2_people) == 2 and num2_people not in num3:
        print('只选了两门课程的人', num2_people)
        num3.append(num2_people)
print(len(num3))

​ e. 求选了三门学生的学生的数量和对应的名字

num4 = 0
for num4_people in course1:
    if num4_people in course2 and num4_people in course3:
        print('选了3门课的人', num4_people)
        num4 += 1
print('总人数', num4)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值