2019-01-03 作业

作业#

使用一个变量all_students保存一个班的学生信息(4个),每个学生需要保存:姓名、年龄、成绩、电话

all_students = [
    {'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
    {'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
    {'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
    {'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
] 

1.添加学生:输入学生信息,将输入的学生的信息保存到all_students中

例如输入:
姓名: 小明
年龄: 20
成绩: 100
电话: 111922  
那么就在all_students中添加{'name':'小明', 'age': 20, 'score': 100, 'tel':'111922'}

all_students = [
{'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
{'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
{'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
{'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
]

student = {'name': '张三', 'age': '年纪', 'score': '成绩','tel':'电话号码'}
message = input('请输入信息:')

for message in student:
if message == 'name':
new_name= input('请输入名字:')
student['name'] = new_name
elif message == 'age':
new_age = int(input('请输入年龄:'))
student['age'] = new_age
elif message == 'score':
new_score = int(input('请输入成绩:'))
student['score'] = new_score
else:
new_tel = input('请输入电话号码:')
student['tel'] = new_tel

print(student)

all_students.append(student)
print(all_students)

2.按姓名查看学生信息:

例如输入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'

all_students = [
{'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
{'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
{'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
{'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
{'name': 'xiaom', 'age': 20, 'score': 100, 'tel': '111922'}
]
k = input('请输入名字')
for one in all_students:
if k != one['name'] :
continue
else:
print(one)
3.求所有学生的平均成绩和平均年龄
all_students = [
{'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
{'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
{'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
{'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
{'name': 'xiaom', 'age': 20, 'score': 100, 'tel': '111922'}
]
ages = 0
scores = 0
for one in all_students:
sum_age = one['age'] + ages
ages = sum_age
sum_score = one['score'] + scores
scores = sum_score
ave_age = ages / int(len(all_students))
ave_score = scores / int(len(all_students))
print(ave_age,ave_score)

4.删除班级中年龄小于18岁的学生
all_students = [
{'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
{'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
{'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
{'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
{'name': 'xiaom', 'age': 20, 'score': 100, 'tel': '111922'}
]

for one in all_students[:]:
if int(one['age']) < 18:
all_students.remove(one)
print(all_students)

5.统计班级中不及格的学生的人数
all_students = [
{'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
{'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
{'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
{'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
{'name': 'xiaom', 'age': 20, 'score': 100, 'tel': '111922'}
]
count = 0
for one in all_students[:]:
if int(one['score']) < 60:
count += 1
print('不及格的学生人数',count)

6.打印手机号最后一位是2的学生的姓名
all_students = [
{'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'},
{'name':'stu2', 'age': 29, 'score':90, 'tel':'211222'},
{'name':'stu3', 'age': 12, 'score':67, 'tel':'521114'},
{'name':'stu4', 'age': 30, 'score':45, 'tel':'900012'},
{'name': 'xiaom', 'age': 20, 'score': 100, 'tel': '111922'}
]

for one in all_students[:]:
if int(one['tel']) % 10 == 2:
print(one['name'])

你可能感兴趣的:(2019-01-03 作业)