2019-01-03 作業(yè)

作業(yè)#

使用一個變量all_students保存一個班的學(xué)生信息(4個),每個學(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'},
] 

1.添加學(xué)生:輸入學(xué)生信息,將輸入的學(xué)生的信息保存到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': '年紀(jì)', '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.按姓名查看學(xué)生信息:

例如輸入:
姓名: 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.求所有學(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'},
{'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歲的學(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'},
{'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.統(tǒng)計班級中不及格的學(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'},
{'name': 'xiaom', 'age': 20, 'score': 100, 'tel': '111922'}
]
count = 0
for one in all_students[:]:
if int(one['score']) < 60:
count += 1
print('不及格的學(xué)生人數(shù)',count)

6.打印手機(jī)號最后一位是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'},
{'name': 'xiaom', 'age': 20, 'score': 100, 'tel': '111922'}
]

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容