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中
message = input('請(qǐng)輸入指令:')
student = {}
if message == '添加學(xué)生':
name = input('姓名:')
student['name'] = name
age = int(input('年齡:'))
student['age'] = age
score = int(input('成績(jī):'))
student['score'] = score
tel = input('電話:')
student['tel'] = tel
all_students.append(student)
print(all_students)
2.按姓名查看學(xué)生信息
message = input('請(qǐng)輸入指令:')
if message == '查看學(xué)生信息':
count = 0
name = input('請(qǐng)輸入學(xué)生姓名:')
for student in all_students:
count += 1
for value in student.values():
if name == value:
print(all_students[count-1])
3.求所有學(xué)生的平均成績(jī)和平均年齡
message = input('請(qǐng)輸入指令:')
if message == '求所有學(xué)生的平均成績(jī)':
scores = []
count = 0
for student in all_students:
scores.append(student['score'])
count += 1
print('所有學(xué)生的平均成績(jī)是:%.2f' % (sum(scores) / count))
if message == '求所有學(xué)生的平均年齡':
ages = []
count = 0
for student in all_students:
ages.append(student['age'])
count += 1
print('所有學(xué)生的平均年齡是:%.2f' % (sum(ages) / count))
4.刪除班級(jí)中年齡小于18歲的學(xué)生
count = 0
for student in all_students:
if student['age'] >= 18:
count += 1
else:
del all_students[count]
print(all_students)
5.統(tǒng)計(jì)班級(jí)中不及格的學(xué)生的人數(shù)
count = 0
for student in all_students:
if student['score'] < 60:
count +=1
print('不及格的人數(shù)是:%d人' % (count))
6.打印手機(jī)號(hào)最后一位是2的學(xué)生的姓名
tels = []
names = []
for student in all_students:
count = 0
names.append(student['name'])
tels.append(student['tel'])
for tel in tels:
if tel[-1] == '2':
print(names[count])
count += 1
else:
count += 1
學(xué)生管理系統(tǒng)
jieMian = """
===========================================
??歡迎用戶:
? 1. 添加學(xué)生
? 2. 查看學(xué)生
? 3. 修改學(xué)生信息
? 4. 刪除學(xué)生
? 5. 返回
============================================
"""
all_students = []
count = 0
print(jieMian)
message = int(input('請(qǐng)選擇(1-5):'))
while True:
if message == 1:
student = {}
input_name = input('請(qǐng)輸入學(xué)生姓名:')
input_age = int(input('請(qǐng)輸入學(xué)生年齡:'))
input_tel = int(input('請(qǐng)輸入學(xué)生電話:'))
student['姓名'] = input_name
student['年齡'] = input_age
student['電話'] = input_tel
all_students.append(student)
print('添加成功倦挂!')
print('1.繼續(xù)')
print('2.返回')
message = int(input('請(qǐng)選擇(1-2):'))
if message == 2:
print(jieMian)
message = int(input('請(qǐng)選擇(1-5):'))
if message == 2:
print('1.查看所有學(xué)生')
print('2.按姓名查找')
print('3.按學(xué)號(hào)查找')
print('4.返回')
message = int(input('請(qǐng)選擇(1-4):'))
if message == 1:
for student in all_students:
name = student['姓名']
age = student['年齡']
tel = student['電話']
count += 1
print('學(xué)號(hào):' + 'stu' + str(count).zfill(3) + ' ' + '姓名:' + name + ' ' + '年齡:' + str(age) + ' ' + '電話:' + str(tel))
print('1.查看所有學(xué)生')
print('2.按姓名查找')
print('3.按學(xué)號(hào)查找')
print('4.返回')
message = int(input('請(qǐng)選擇(1-4):'))
if message == 4:
print(jieMian)
message = int(input('請(qǐng)選擇(1-5):'))
if message == 3:
print('1.')
print('2.')
print('3.')
print('4.')
message = int(input('請(qǐng)選擇(1-4):'))
if message == 4:
print('1.')
print('2.')
print('3.')
print('4.')
message = int(input('請(qǐng)選擇(1-4):'))
if message == 5:
exit()