使用一個變量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': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
{'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
{'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
{'name': '小紅', 'age': 30, 'score': 45, 'tel': '900012'},
]
student = {}
name = input('請輸入學生的姓名:')
student['name'] = name
age = int(input('請輸入學生的年齡:'))
student['age'] = age
score = float(input('請輸入學生的成績:'))
student['score'] = score
tel = input('請輸入學生的電話')
student['tel'] = tel
all_students.append(student)
print(all_students)
2.按姓名查看學生信息:
例如輸入: 姓名: stu1 就打铀 :'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'
all_students = [
{'name': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
{'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
{'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
{'name': '小紅', 'age': 30, 'score': 45, 'tel': '900012'},
]
name_message = input('請輸入要查看的學生的姓名:')
for student in all_students:
if student['name'] == name_message:
print(student)
break
else:
print('該學生不存在')
3.求所有學生的平均成績和平均年齡
all_students = [
{'name': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
{'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
{'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
{'name': '小紅', 'age': 30, 'score': 45, 'tel': '900012'},
]
sum_scores = 0
sum_ages = 0
for student in all_students:
sum_scores += student['score']
sum_ages += student['age']
print('所有學生的平均成績?yōu)?.2f' % (sum_scores / len(all_students)))
print('所有學生的平均年齡為%.2f' % (sum_ages / len(all_students)))
4.刪除班級中年齡小于18歲的學生
all_students = [
{'name': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
{'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
{'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
{'name': '小紅', 'age': 30, 'score': 45, 'tel': '900012'},
]
index = 0
while index < len(all_students):
if all_students[index]['age'] < 18:
del all_students[index]
else:
index += 1
print(all_students)
5.統(tǒng)計班級中不及格的學生的人數(shù)
all_students = [
{'name': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
{'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
{'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
{'name': '小紅', 'age': 30, 'score': 45, 'tel': '900012'},
]
count = 0
for index in range(len(all_students)):
if all_students[index]['score'] < 60:
count += 1
print('班級中不及格的學生的人數(shù)為%d' % count)
6.打印手機號最后一位是2的學生的姓名
all_students = [
{'name': '小花', 'age': 19, 'score': 81, 'tel': '192222'},
{'name': '小白', 'age': 29, 'score': 90, 'tel': '211222'},
{'name': '小黑', 'age': 12, 'score': 67, 'tel': '521114'},
{'name': '小紅', 'age': 30, 'score': 45, 'tel': '900012'},
]
for student in all_students:
if student['tel'][len(student['tel']) - 1] == '2':
print(student['name'])