import os
# -*- coding:utf-8 -*-
menus="""
1.添加学生信息
2.修改学生信息
3.删除学生信息
4.查询学生信息
5.遍历所有学生信息
6.退出学生管理系统
0.清除屏幕信息
"""
print("\t学生管理系统v.1.0.0\n")
g_student_dict = {}
#以字典形式存储学生信息函数(id,name,sex,age)
def add_student():
'''学生id作为字典键'''
student_inf={}
#id_list=list(g_student_dict.keys())
while True:
id=input("\t请输入学生学号:")
if id in g_student_dict.keys():
print("学号[%s]已存在!" %(id))
continue
else:
nm=input("\t请输入学生姓名:")
student_inf['name'] = nm
sex=input("\t请输入学生性别(0-女,1-男):")
student_inf['sex'] = sex
age=input("\t请输入学生年龄:")
student_inf['age'] = age
g_student_dict[id]=student_inf
break
#根据学生id进行数据修改
def update_student():
id=input("\t请输入要修改学生学号:")
if id not in g_student_dict.keys():
print("不存在[%s]学号" % (id))
else:
print("注意:回车代表默认不修改!")
nm=input("\t请输入[%s]学号的学生姓名:" %(id))
if len(nm):
g_student_dict[id]['name'] = nm
sex=input("\t请输入[%s]学号的学生性别(0-女,1-男):" %(id))
if len(sex):
g_student_dict[id]['sex'] = sex
age = input("\t请输入[%s]学号的学生年龄:" %(id))
if len(age):
g_student_dict[id]['age'] = age
def delete_student():
id=input("\t请输入要删除的学生学号:")
if id in g_student_dict.keys():
del g_student_dict[id]
print("学号[%s]学生信息已删除!" %(id))
else:
print("不存在[%s]学号" %(id))
def query_student():
id = input("\t请输入要查询的学生学号:")
if id in g_student_dict.keys():
print('{:<20}{:<20}{:<20}'.format('id', 'name', 'sex'), end='')
print('{:<20}'.format('age'))
print('{:<20}'.format(id), end='')
print('{:<20}'.format(g_student_dict[id]['name']), end='')
print('{:<20}'.format(g_student_dict[id]['sex']), end='')
print('{:<20}'.format(g_student_dict[id]['age']))
else:
print("不存在[%s]学号" % (id))
def traverse_student():
if len(g_student_dict):
print('{:<20}'.format('id'), end='')
print('{:<20}'.format('name'), end='')
print('{:<20}'.format('sex'), end='')
print('{:<20}'.format('age'))
for id in g_student_dict.keys():
print('{:<20}'.format(id), end='')
print('{:<20}'.format(g_student_dict[id]['name']), end='')
print('{:<20}'.format(g_student_dict[id]['sex']), end='')
print('{:<20}'.format(g_student_dict[id]['age']))
else:
print('无记录')
#将字典中每个学生对象单独存储一行到文件中
def write_file(**g_student_dict):
with open('G:\workspace\student.txt', 'w+') as fp:
for key, value in g_student_dict.items():
student_dict_tmp={}
student_dict_tmp[key]=value
fp.write(str(student_dict_tmp)+'\n')
#先读取文件中并存储到g_student_dict字典中
def read_file():
student_dict_tmp = {}
try:
with open('G:\workspace\student.txt', 'r+') as fp:
for line in fp.readlines():
line=line.strip()
student_dict_tmp.update(eval(line))
g_student_dict.update(student_dict_tmp)
except IOError:
print('无存量数据')
read_file()
print(g_student_dict)
while True:
print(menus)
num = input("请输入菜单编号:")
if num.isdigit():
if int(num) not in range(0,7):
print("菜单编号输入错误,请重新输入!")
continue
else:
if int(num)==1:
add_student()
elif int(num)==2:
update_student()
elif int(num)==3:
delete_student()
elif int(num)==4:
query_student()
elif int(num)==5:
traverse_student()
elif int(num)==6:
write_file(**g_student_dict)
break
elif int(num)==0:
os.system('cls')
else:
print("输入编号错误")
else:
print("请输入数字菜单编号")
学生管理系统v.1.0.0
无存量数据
1.添加学生信息
2.修改学生信息
3.删除学生信息
4.查询学生信息
5.遍历所有学生信息
6.退出学生管理系统
0.清除屏幕信息
请输入菜单编号: