1.写一个程序
a.用一个变量来保存一个班级的学生信息,学生信息:姓名、学号、成绩(英语、体育、美术、数学)、年龄
b.可以给这个班级添加学生
c.根据姓名查看班级里的某个学生的信息
d.根据姓名删除一个指定的学生信息
e.查看班级所有的学生的信息
f.求指定学生的平均成绩
import os
def screen_clear():
os.system('cls')
# 定义一个列表来保存学生信息
stu_info = [{'name':'joe', 'stu_id':'py1805001', 'score':{'English':'80', 'PE':'70', 'art':'75', 'math':'85'}, 'age':'22'},
{'name':'re', 'stu_id':'py1805002', 'score':{'English':'81', 'PE':'71', 'art':'76', 'math':'86'}, 'age':'21'}]
eye = False
while True:
# 清屏
screen_clear()
# 打印信息
print('''
欢迎来到学生信息系统!
1.添加学生信息
2.根据学号查看个人信息
3.根据学号删除一个指定的学生信息
4.查看所有学生的信息
5.求指定学号的学生的平均成绩
输入"exit"退出程序
''')
# 用户输入
user_cmd = input('>>>>>')
if user_cmd == '1':
# 清屏
screen_clear()
stu_id_new = input('请输入学生的学号:')
for index_info in range(len(stu_info)):
if stu_id_new in stu_info[index_info]['stu_id']:
print('该学生信息已存在!')
eye = True
break
if eye:
continue
name_new = input('请输入学生的姓名:')
score_new_english = input('请输入音乐成绩:')
score_new_pe = input('请输入体育成绩:')
score_new_art = input('请输入美术成绩:')
score_new_math = input('请输入数学成绩:')
age_new = input('请输入学生的年龄:')
# 插入数据
stu_info.append({'name':name_new, 'stu_id':stu_id_new,
'score':{'English':score_new_english, 'PE':score_new_pe, 'art':score_new_art, 'math':score_new_math},
'age':age_new})
elif user_cmd == '2':
stu_id_retrieve = input('请输入学生的学号:')
for index_info in range(len(stu_info)):
# 查找学生
if stu_id_retrieve in stu_info[index_info]['stu_id']:
# 清屏
screen_clear()
# 打印学生信息
print('姓名:%s\t学号:%s\t英语:%s\t体育:%s\t美术:%s\t数学:%s\t年龄:%s\t'%(stu_info[index_info]['name'],
stu_info[index_info]['stu_id'],stu_info[index_info]['score']['English'],
stu_info[index_info]['score']['PE'],stu_info[index_info]['score']['art'],
stu_info[index_info]['score']['math'],stu_info[index_info]['age'] ))
break
elif not stu_id_retrieve in stu_info[index_info]['stu_id']:
print('不存在该学生!')
elif user_cmd == '3':
stu_id_del = input('请输入学生的学号:')
for index_info in range(len(stu_info)):
# 查找学生
if stu_id_del in stu_info[index_info]['stu_id']:
del stu_info[index_info]
print('删除信息成功!')
break
elif user_cmd == '4':
for index_info in range(len(stu_info)):
print('姓名:%s\t学号:%s\t英语:%s\t体育:%s\t美术:%s\t数学:%s\t年龄:%s\t' % (stu_info[index_info]['name'],
stu_info[index_info]['stu_id'],stu_info[index_info]['score']['English'],
stu_info[index_info]['score']['PE'],stu_info[index_info]['score']['art'],
stu_info[index_info]['score']['math'],stu_info[index_info]['age']))
elif user_cmd == '5':
stu_id_ave = input('请输入学生的学号:')
for index_info in range(len(stu_info)):
# 查找学生
if stu_id_ave in stu_info[index_info]['stu_id']:
score_ave = (int(stu_info[index_info]['score']['English'])+int(stu_info[index_info]['score']['PE'])
+int(stu_info[index_info]['score']['art'])+int(stu_info[index_info]['score']['math']))/4
print('姓名:%s\t学号:%s\t平均成绩是:%d'%(stu_info[index_info]['name'],
stu_info[index_info]['stu_id'],score_ave))
elif user_cmd == 'exit':
break
else:
print('输入内容有误!')
2.学生管理系统
stu_infos = [{'name':'joe', 'stu_id':'py1805001', 'score':{'English':'80', 'PE':'70', 'art':'75', 'math':'85'},
'age':'22'},
{'name':'re', 'stu_id':'py1805002', 'score':{'English':'81', 'PE':'71', 'art':'76', 'math':'86'},
'age':'21'}]
def list_main():
print('''
欢迎来到学生信息系统!
1.添加学生信息
2.根据学号查看个人信息
3.根据学号删除一个指定的学生信息
4.查看所有学生的信息
5.求指定学号的学生的平均成绩
输入"exit"退出程序
''')
user_cmd = input('>>>>>')
return user_cmd
# 判断用户输入
def user_input(user_cmd):
if user_cmd == '1':
stu_info = stu_find()
if not isinstance(stu_info, str):
print('该学生信息已存在!')
return
name, stu_id, english, pe, art, math, age = stu_info_input(stu_info)
stu_create(name, stu_id, english, pe, art, math, age)
# 判断是否继续或者退出
flag = '1'
redo(flag)
elif user_cmd == '2':
stu_info = stu_find()
if isinstance(stu_info, str):
print('学生信息不存在!')
else:
# 打印学生信息
print('姓名:%s\t学号:%s\t英语:%s\t体育:%s\t美术:%s\t数学:%s\t年龄:%s\t' % (stu_info['name'],
stu_info['stu_id'],stu_info['score']['English'],stu_info['score']['PE'],
stu_info['score']['art'],stu_info['score']['math'],stu_info['age']))
# 判断是否继续或者退出
flag = '2'
redo(flag)
elif user_cmd == '3':
stu_info = stu_find()
if isinstance(stu_info, str):
print('学生信息不存在!')
else:
stu_del(stu_info)
# 判断是否继续或者退出
flag = '3'
redo(flag)
elif user_cmd == '4':
stu_find_all()
# 判断是否继续或者退出
flag = '4'
redo(flag)
elif user_cmd == '5':
stu_info = stu_find()
if isinstance(stu_info, str):
print('学生信息不存在!')
else:
score_ave(stu_info)
# 判断是否继续或者退出
flag = '5'
redo(flag)
elif user_cmd == 'exit':
exit(0)
else:
print('输入内容有误!')
# 查找学生
def stu_find():
stu_info = input('请输入学生的学号:')
for index_info in range(len(stu_infos)):
# 学生信息存在
if stu_info == stu_infos[index_info]['stu_id']:
return stu_infos[index_info]
# 学生信息不存在
return stu_info
# 查找全部
def stu_find_all():
for index_info in range(len(stu_infos)):
print('姓名:%s\t学号:%s\t英语:%s\t体育:%s\t美术:%s\t数学:%s\t年龄:%s\t' % (stu_infos[index_info]['name'],
stu_infos[index_info]['stu_id'],
stu_infos[index_info]['score']['English'],
stu_infos[index_info]['score']['PE'],
stu_infos[index_info]['score']['art'],
stu_infos[index_info]['score']['math'],
stu_infos[index_info]['age']))
# 用户输入学生信息
def stu_info_input(stu_info):
stu_id = stu_info
name = input('请输入学生的姓名:')
english = input('请输入音乐成绩:')
pe = input('请输入体育成绩:')
art = input('请输入美术成绩:')
math = input('请输入数学成绩:')
age = input('请输入学生的年龄:')
return name, stu_id, english, pe, art, math, age
# 添加学生信息
def stu_create(name, stu_id, english, pe, art, math, age):
stu_infos.append({'name':name, 'stu_id':stu_id,'score':{'English':english, 'PE':pe, 'art':art, 'math':math},
'age':age})
# 删除学生信息
def stu_del(stu_info):
index_stu = stu_infos.index(stu_info)
del stu_infos[index_stu]
print('删除信息成功!')
# 平均成绩
def score_ave(stu_info):
score_ave = (int(stu_info['score']['English']) + int(stu_info['score']['PE'])
+ int(stu_info['score']['art']) + int(stu_info['score']['math'])) / 4
print('姓名:%s\t学号:%s\t平均成绩是:%d' % (stu_info['name'],
stu_info['stu_id'], score_ave))
#
def redo(flag):
if flag =='1':
user_cmd = input('''
1.继续添加
2.返回上一层
>>>>>
''')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('输入有误!')
elif flag == '2':
user_cmd = input('''
1.继续查看
2.返回上一层
>>>>>
''')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('输入有误!')
elif flag == '3':
user_cmd = input('''
1.继续删除
2.返回上一层
>>>>>
''')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('输入有误!')
elif flag == '4':
user_cmd = input('''
1.继续查看
2.返回上一层
>>>>>
''')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('输入有误!')
elif flag == '5':
user_cmd = input('''
1.继续查询
2.返回上一层
>>>>>''')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('输入有误!')
# 主程序
def main():
while True:
# 打印列表内容,等待用户输入
user_input(list_main())
# 开始程序
if __name__ == '__main__':
main()
ps:变着花样的CRUD