day 08宿題
2234 ワード
変数all_を使用studentsはクラスの学生情報(4つ)を保存し、学生ごとに名前、年齢、成績、電話を保存する必要があります.
1.生徒の追加:生徒情報を入力し、入力した生徒の情報をall_に保存するstudentsでは、例えば名前:明ちゃん年齢:20成績:100電話:111922でall_studentsに{'name':'明ちゃん','age':20,'score':100,'tel':'111922'}を追加
2.名前による学生情報の表示:
例えば入力:名前:stu 1で印刷:'name':'stu 1','age':19,'score':81,'tel':'192222'
3.すべての学生の平均成績と平均年齢を求める
4.クラスの18歳未満の生徒を削除
5.クラスで不合格になった学生の人数を集計する
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'},
]
1.生徒の追加:生徒情報を入力し、入力した生徒の情報をall_に保存するstudentsでは、例えば名前:明ちゃん年齢:20成績:100電話:111922でall_studentsに{'name':'明ちゃん','age':20,'score':100,'tel':'111922'}を追加
name = input(' :')
age = input(' :')
score = input(' :')
num = input(' :')
add_student = {'name': name, 'age': age, 'score': score, 'tel': num}
all_students.append(add_student)
print(all_students)
2.名前による学生情報の表示:
例えば入力:名前:stu 1で印刷:'name':'stu 1','age':19,'score':81,'tel':'192222'
message = input(' :')
for index in all_students:
if index['name'] == message:
print(index)
:
{'name': ' ', 'age': 20, 'score': 100, 'tel': '111922'}
3.すべての学生の平均成績と平均年齢を求める
all_score = 0 #
person = 0 #
ages = 0 #
for index in all_students:
all_score += index['score']
ages += index['age']
person += 1
print(all_score / person, ages / person)
4.クラスの18歳未満の生徒を削除
for index in all_students[:]:
if index['age'] < 18:
all_students.remove(index)
print(all_students)
5.クラスで不合格になった学生の人数を集計する
pass_student = 0
for index in all_students:
if index['score'] < 60:
pass_student += 1
print(pass_student)
6.携帯番号を印刷する最後の学生の名前は2です.
for index in all_students:
if int(index['tel'][-1]) == 2:
print(index['name'])