使用一個(gè)變量all_students保存一個(gè)班的學(xué)生信息(4個(gè))强缘,每個(gè)學(xué)生需要保存:姓名环葵、年齡算柳、成績(jī)抛蚤、電話
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'},
]
all_studens = []
for x in range(1,5):
new_name = input('請(qǐng)輸入第%位學(xué)生姓名:' % x)
new_age = int(input('請(qǐng)輸入第%位學(xué)生年齡:'% x))
new_score = int(input('請(qǐng)輸入第%位學(xué)生成績(jī):'% x))
new_tel = int(input('請(qǐng)輸入第%位學(xué)生電話:'% x))
dict1 = {'name':new_name,'age':new_age,'score':new_score,'tel':new_tel}
all_studens.append(dict1)
print(all_studens)
1.添加學(xué)生:輸入學(xué)生信息台谢,將輸入的學(xué)生的信息保存到all_students中
while True:
char = input('是否輸入信息:')
if char == '是':
name = input('輸入姓名:')
age = int(input('輸入年齡:'))
score = int(input('輸入成績(jī):'))
tel = int(input('輸入電話:'))
dict1 = {'name':name,'age':age,'score':score,'tel':tel}
print(dict1)
例如輸入:
姓名: 小明
年齡: 20
成績(jī): 100
電話: 111922
那么就在all_students中添加{'name':'小明', 'age': 20, 'score': 100, 'tel':'111922'}
2.按姓名查看學(xué)生信息:
例如輸入:
姓名: stu1 就打印:'name':'stu1', 'age': 19, 'score':81, 'tel':'192222'
list1 = [
{'name':'stu1','age':18,'score':89,},
{'name':'stu2','age':28,'score':65,},
{'name':'stu3','age':23,'score':75,},
{'name':'stu4','age':25,'score':99,}
]
new_name = input('輸入學(xué)生姓名:')
count = 0
for dict1 in list1:
if new_name == dict1['name']
print(dict1)
count += 1
if count == 0:
print('沒(méi)有該學(xué)生')
3.求所有學(xué)生的平均成績(jī)和平均年齡
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'},
]
new_age = 0
new_score = 0
for dict1 in all_studens:
age = dict1['age']
score = dict1['score']
new_age += age
new_score += score
avre_age = new_age / len(all_studens)
avre _score = new_score / len(all_studens)
print('所有學(xué)生的平均成績(jī)?yōu)椋?.1f,平均年齡:%.1f' % (avre_score,avre_age))
4.刪除班級(jí)中年齡小于18歲的學(xué)生
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'},
]
for dict1 in all_students:
if dict1['age'] < 18:
dict1.clear()
print(all_students)
5.統(tǒng)計(jì)班級(jí)中不及格的學(xué)生的人數(shù)
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'},
]
num = 0
for dict1 in all_students:
if dict1['score'] <60:
num += 1
print('班級(jí)中不及格的學(xué)生的人數(shù):%d' % num)
6.打印手機(jī)號(hào)最后一位是2的學(xué)生的姓名
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'},
]
for dict1 in all_students:
char = dict1['tel']
if char[-1] == '2':
print(dict1['name'])