Day08-作業(yè)

使用一個變量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'])
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蓝纲,隨后出現(xiàn)的幾起案子阴孟,更是在濱河造成了極大的恐慌,老刑警劉巖税迷,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件永丝,死亡現(xiàn)場離奇詭異,居然都是意外死亡箭养,警方通過查閱死者的電腦和手機慕嚷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人喝检,你說我怎么就攤上這事嗅辣。” “怎么了挠说?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵澡谭,是天一觀的道長。 經(jīng)常有香客問我纺涤,道長译暂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任撩炊,我火速辦了婚禮外永,結果婚禮上,老公的妹妹穿的比我還像新娘拧咳。我一直安慰自己伯顶,他們只是感情好,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布骆膝。 她就那樣靜靜地躺著祭衩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪阅签。 梳的紋絲不亂的頭發(fā)上掐暮,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機與錄音政钟,去河邊找鬼路克。 笑死,一個胖子當著我的面吹牛养交,可吹牛的內(nèi)容都是我干的精算。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼碎连,長吁一口氣:“原來是場噩夢啊……” “哼灰羽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鱼辙,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤廉嚼,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后倒戏,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體前鹅,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年峭梳,在試婚紗的時候發(fā)現(xiàn)自己被綠了舰绘。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蹂喻。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖捂寿,靈堂內(nèi)的尸體忽然破棺而出口四,到底是詐尸還是另有隱情,我是刑警寧澤秦陋,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布蔓彩,位于F島的核電站,受9級特大地震影響驳概,放射性物質(zhì)發(fā)生泄漏赤嚼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一顺又、第九天 我趴在偏房一處隱蔽的房頂上張望更卒。 院中可真熱鬧,春花似錦稚照、人聲如沸蹂空。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽上枕。三九已至,卻和暖如春弱恒,著一層夾襖步出監(jiān)牢的瞬間辨萍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工返弹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留锈玉,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓琉苇,卻偏偏與公主長得像,于是被迫代替她去往敵國和親悦施。 傳聞我的和親對象是個殘疾皇子并扇,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 使用一個變量all_students保存一個班的學生信息(4個),每個學生需要保存:姓名抡诞、年齡穷蛹、成績、電話 1.添...
    LittleBear_6c91閱讀 216評論 0 1
  • 使用一個變量all_students保存一個班的學生信息(4個)昼汗,每個學生需要保存:姓名肴熏、年齡、成績顷窒、電話 1.添...
    饅頭不要面閱讀 62評論 0 0
  • 8月22日-----字符串相關 2-3 個性化消息: 將用戶的姓名存到一個變量中蛙吏,并向該用戶顯示一條消息源哩。顯示的消...
    future_d180閱讀 979評論 0 1
  • 一、面向?qū)ο缶幊叹幊趟枷耄?.面向過程編程 ---> 算法鸦做,邏輯(數(shù)學邏輯)2.函數(shù)式編程 ---> 函數(shù)励烦,模塊3...
    Smr_T閱讀 520評論 0 0
  • 一、快捷鍵 ctr+b 執(zhí)行ctr+/ 單行注釋ctr+c ...
    o_8319閱讀 5,828評論 2 16