1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话
student = {'name': '小伊', 'age': 18, 'score': 90, 'tel': 15181694008}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = [
{'name': '小伊', 'age': 17, 'score': 92, 'tel': 15181694008},
{'name': '小耳', 'age': 17, 'score': 91, 'tel': 15181694018},
{'name': '小伞', 'age': 18, 'score': 45, 'tel': 15181694003},
{'name': '小司', 'age': 18, 'score': 99, 'tel': 15181694004},
{'name': '小武', 'age': 17, 'score': 54, 'tel': 15181694005},
{'name': '小刘', 'age': 19, 'score': 97, 'tel': 15181694006}
]
a.统计不及格学生的个数
fail_number = 0
for dict_stu in students:
if dict_stu['score'] < 60:
fail_number += 1
print('不及格人数为:', fail_number)
b.打印不及格学生的名字和对应的成绩
for dict_stu in students:
if dict_stu['score'] < 60:
print(dict_stu['name'], '的成绩为', dict_stu['score'], '不及格')
c.统计未成年学生的个数
Juveniles = 0
for dict_stu in students:
if dict_stu['age'] < 18:
Juveniles += 1
print('有', Juveniles, '个未成年人')
d.打印手机尾号是8的学生的名字
print('手机尾号是8的学生有:', end='')
for dict_stu in students:
tel = str(dict_stu['tel'])
if tel[-1] == '8':
print(dict_stu['name'], end=' ')
print()
e.打印最高分和对应的学生的名字
best_score = 0
best_name = ''
for dict_stu in students:
if dict_stu['score'] > best_score:
best_score = dict_stu['score']
for dict_stu in students:
if dict_stu['score'] == best_score:
best_name += dict_stu['name']
print(best_name, '考得最高分', best_score)
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
for i in range(len(students)):
for j in range(i+1, len(students)):
if students[j]['score'] > students[i]['score']:
students[j], students[i] = students[i], students[j]
j += 1
for dict_stu in students:
print(dict_stu)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
subject_1 = ['Tom', 'Jack', 'IceIce']
subject_2 = ['Tony', 'Jack', 'Rose']
subject_3 = ['Tom', 'Tony', 'Jack']
a.求选课学生总共有多少人
print('共有', len(set(subject_1 + subject_2 + subject_3)), '名学生选课')
b.求只选了第一个学科的人的数量和对应的名字
rookie_name = set(subject_1) - set(subject_3) - set(subject_3)
rookie_num = len(rookie_name)
print('只有', end='')
for name in rookie_name:
print(name, end=' ')
print('这', rookie_num, '位同学只选了第一个学科')
c.求只选了一门学科的学生的数量和对应的名字
d.求只选了两门学科的学生的数量和对应的名字
e.求选了三门学生的学生的数量和对应的名字
n = int(input('要查询选了几门学科的学生:'))
stu_list = subject_1 + subject_2 + subject_3
stu_set = set(stu_list)
stu_num = 0
name = []
for stu in stu_set:
if stu_list.count(stu) == n:
stu_num += 1
name.append(stu)
for out_name in name:
print(out_name, end=' ')
print(stu_num, '位同学选了', n, '门课')