day08

2019-04-25  本文已影响0人  运算符
1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话
student = {'name': 'qiangzai', 'age': 18, 'Score': 100, 'tel': '17738724989'}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = []
for x in range(6):
    student = {}
    name = input('请输入姓名:')
    student['name'] = name
    age = int(input('请输入年龄:'))
    student['age'] = age
    score = int(input('请输入成绩:'))
    student['score'] = score
    tel = input('请输入电话:')
    student['tel'] = tel
    students.append(student)

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

count = 0
for student in students:
    if student['score'] < 60:
        count += 1
print("不及格人数:",count)

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

for student in students:
    if student['score'] < 60:
        print("%s不及格,分数为%d" % (student['name'],student['score']))

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

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

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

for student in students:
    if student['tel'][-1] == '8':
        print(student['name'])
    else:
        continue

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

for index in range(len(students)):
    if students[index]['score'] > max:
        max = students[index]['score']
for student in students:
    if student['score'] == max:
        print("最高分%d的学生为%s" % (max, student['name']))

f.将列表按学生成绩从大到小排序

#冒泡法
temp = 0
for i in range(len(students)):
    for j in range(i+1, len(students)):
        if students[i]['score'] < students[j]['score']:
            temp = students[i]
            students[i] = students[j]
            students[j] = temp
print(students)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
Chinese = ['许文强', 'Bob', '小明', '许大炮']
math = ['许文强', '拉斐特', '泰戈尔', '王大锤']
English = ['列夫托尔斯泰', '普金', '王大锤', '许文强']

Chinese = set(Chinese)
math = set(math)
English = set(English)

a. 求选课学生总共有多少人

num = Chinese | math | English
print(len(num))

b. 求只选了第一个学科的人的数量和对应的名字

print("只选了第一个学科的:",Chinese - math - English)
print("有%d人" % len(Chinese - math - English))

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

print(Chinese ^ math ^ English-(Chinese & math & English))
print(len(Chinese ^ math ^ English-(Chinese & math & English)))

d. 求只选了两门学科的学生的数量和对应的名字

print((Chinese & math)|(Chinese &English)|(math & English)-(Chinese & math & English))
print(len((Chinese & math)|(Chinese &English)|(math & English)-(Chinese & math & English)))

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

print(Chinese & math & English)
print(len(Chinese & math & English))
上一篇下一篇

猜你喜欢

热点阅读