day10-homework
2018-09-01 本文已影响0人
rzlong
基于json保存数据的简单学生管理系统
- 需要用到json文件和python格式编码转换的看这
- 控制台终端Terminal密码隐藏输入(不是直接run xx.py)
- 在线json格式校验
- codes
"""
__author__ = 'rz'
__Time__ = 2018/8/31 0031 下午 8:05
"""
import json
import getpass #用于控制台input输入密码隐藏化
def menu():
"""
主功能界面
:return:
"""
print("*"*53)
print('*' + ' '*51 + '*')
print('*' + 'Student Manager System'.center(51,' ') + '*')
print('*' + '1.Log in'.center(51,' ') + '*')
print('*' + '2.Register'.center(51,' ') + '*')
print('*' + '0.Exit '.center(51,' ') + '*')
print('*' + ' ' * 51 + '*')
print("*" * 53)
while True:
option = input('输入选项:')
if option == '1':
login()
elif option == '2':
register()
elif option == '0':
exit()
elif option == '':
continue
else:
print('输入有误,请重新选择功能项!')
def login():
"""
用户登录
:return:
"""
print('*' + 'starting login'.center(50, ' ') + '*')
f1 = open('./rmyfile/system_info.json', 'r', encoding='utf-8')
res = f1.read()
f1.close()
sys_info_list = json.loads(res) # 将文本中的json格式转换成python中格式,此处为list
while True:
t_id = input('输入id:')
t_pwd = getpass.getpass('输入密码:')
for n in range(len(sys_info_list)):
if sys_info_list[n]['id'] == t_id and sys_info_list[n]['pwd'] == t_pwd:
submenu(sys_info_list[n]) ##将该用户的字典信息返回
else:
print('没有该用户或用户密码不匹配,请重新登录')
def register():
"""
用户注册
:return:
"""
print('*' + 'starting register'.center(50, ' ') + '*')
f1 = open('./rmyfile/system_info.json', 'r', encoding='utf-8')
res = f1.read()
f1.close()
sys_info_list = json.loads(res) # 将文本中的json格式转换成python中格式,此处为list
while True:
t_id = input('输入用户id:')
for n in range(len(sys_info_list)):
if t_id == sys_info_list[n]['id']:
print('该用户id已存在.')
break
elif t_id == '':
print('用户id不能为空.')
break
else:
break
t_user = input('输入用户名:')
while True:
t_pwd = getpass.getpass('输入密码:')
t_pwd2 = getpass.getpass('确认密码:')
if t_pwd != t_pwd2:
print('密码不一致')
else:
break
new_info_dict = {}
new_info_dict['id'] = t_id
new_info_dict['user'] = t_user
new_info_dict['pwd'] = t_pwd
new_info_dict['students'] = []
sys_info_list.append(new_info_dict)
sys_info_json = json.dumps(sys_info_list) #将添加完信息的列表转换成json格式
f2 =open('./rmyfile/system_info.json','w',encoding='utf-8')
f2.write(sys_info_json)
print('用户:%s' % t_id + '注册成功')
f2.close()
print('选项:继续注册1/返回上一级0')
while True:
option = input('请输入选项:')
if option == '1':
register()
elif option == '0':
menu()
elif option == '':
continue
else:
print('输入有误,请重新选择.')
def submenu(teacher_dict:dict):
"""
子功能界面-用户登录后
:param teacher_dict:
:return:
"""
print("*" * 53)
print('*' + ('Welcome to you - %s'%teacher_dict['user']).center(50, ' ') + '*')
print('*' + '1.Add student'.center(50, ' ') + '*')
print('*' + '2.Edit student'.center(50, ' ') + '*')
print('*' + '3.Query student'.center(50, ' ') + '*')
print('*' + '4.Delete student'.center(50, ' ') + '*')
print('*' + '5.Return menu'.center(50, ' ') + '*')
print('*' + '0.Exit'.center(50, ' ') + '*')
print("*" * 53)
while True:
option = input('输入选项:')
if option == '1':
add_student(teacher_dict)
elif option == '2':
edit_student(teacher_dict)
elif option == '3':
query_student(teacher_dict)
elif option == '4':
delete_student(teacher_dict)
elif option == '5':
menu()
elif option == '0':
exit()
elif option == '':
continue
else:
print('输入有误,请重新选择功能项!')
def add_student(teacher_dict:dict):
print('*' + 'Adding student'.center(50, ' ') + '*')
f1 = open('./rmyfile/system_info.json', 'r', encoding='utf-8')
res = f1.read()
f1.close()
sys_info_list = json.loads(res) # 将文本中的json格式转换成python中格式,此处为list
stu_name = input('输入学生姓名:')
while True:
stu_id = input('输入学生学号:')
if stu_id == '':
continue
for n in range(len(teacher_dict['students'])): #学号一定不能相同
if stu_id == teacher_dict['students'][n]['stuid']:
print('学号已被占用,请重置.')
break
else: #注意若是上面的break结束循环,是不会执行else的
break
age = input('输入年龄:')
phone = input('输入电话号码:')
student_dcit = {'stuname':stu_name,'stuid':stu_id,'age':age,'phone':phone} #生成的新学生字典
t_index = sys_info_list.index(teacher_dict) #教师字典信息在系统信息列表的索引
sys_info_list[t_index]['students'].append(student_dcit) #添加新学生到系统信息列表
sys_info_json = json.dumps(sys_info_list)
f2 = open('./rmyfile/system_info.json', 'w', encoding='utf-8')
f2.write(sys_info_json)
f2.close()
print('添加学生( %s )成功.\n' % stu_name)
print('选项:继续添加1/返回上一级0')
while True:
option = input('请输入选项:')
if option == '1':
add_student(sys_info_list[t_index])
elif option == '0':
submenu(sys_info_list[t_index])
elif option == '':
continue
else:
print('输入有误,请重新选择.\n')
def edit_student(teacher_dict:dict):
print('*' + 'Editing student'.center(50, ' ') + '*')
print('你的所有学生信息如下:')
for n in range(len(teacher_dict['students'])): #打印全部学生
print(teacher_dict['students'][n])
f1 = open('./rmyfile/system_info.json', 'r', encoding='utf-8')
res = f1.read()
f1.close()
sys_info_list = json.loads(res) # 将文本中的json格式转换成python中格式,此处为list
t_index = sys_info_list.index(teacher_dict) # 教师字典信息在系统信息列表的索引
while True:
edit_id = input('输入需要修改的学生id:') # 先不做输入得不到学生id的情形
mark = 0
if edit_id == '':
continue
for n in range(len(teacher_dict['students'])):
if teacher_dict['students'][n]['stuid'] == edit_id: #找到匹配的学生
edit_name = input('修改名字:')
edit_age = input('修改年龄:')
edit_phone = input('修改电话号码:')
teacher_dict['students'][n]['stuname'] = edit_name
teacher_dict['students'][n]['age'] = edit_age
teacher_dict['students'][n]['phone'] = edit_phone
sys_info_list[t_index]['students'][n] = teacher_dict['students'][n].copy() #修改对应的学生的字典
mark = 1
sys_info_json = json.dumps(sys_info_list)
f2 = open('./rmyfile/system_info.json', 'w', encoding='utf-8')
f2.write(sys_info_json)
f2.close()
print('修改学生信息成功.\n')
break
if not mark:
print('欲修改的学生不存在( %s ).\n' % edit_id)
break
print('选项:继续修改1/返回上一级0')
while True:
option = input('请输入选项:')
if option == '1':
edit_student(sys_info_list[t_index])
elif option == '0':
submenu(sys_info_list[t_index])
elif option == '':
continue
else:
print('输入有误,请重新选择.')
def delete_student(teacher_dict:dict):
print('*' + 'Deleting student'.center(50, ' ') + '*')
print('你的所有学生信息如下:')
for n in range(len(teacher_dict['students'])): # 打印全部学生
print(teacher_dict['students'][n])
f1 = open('./rmyfile/system_info.json', 'r', encoding='utf-8')
res = f1.read()
f1.close()
sys_info_list = json.loads(res) # 将文本中的json格式转换成python中格式,此处为list
t_index = sys_info_list.index(teacher_dict) # 教师字典信息在系统信息列表的索引
while True:
del_id = input('输入需要删除的学生id:') # 先不做输入得不到学生id的情形
mark = 0
if del_id == '':
continue
for n in range(len(teacher_dict['students'])):
if teacher_dict['students'][n]['stuid'] == del_id: # 找到匹配的学生id
sys_info_list[t_index]['students'].pop(n) #删除对应学生的字典
sys_info_json = json.dumps(sys_info_list)
f2 = open('./rmyfile/system_info.json', 'w', encoding='utf-8')
f2.write(sys_info_json)
f2.close()
print('删除学生( %s )成功.\n' % del_id)
mark = 1
break
if not mark: #若没有这名学生给提示信息
print('没有id( %s )的学生\n' % del_id)
break
print('选项:继续删除1/返回上一级0')
while True:
option = input('请输入选项:')
if option == '1':
delete_student(sys_info_list[t_index])
elif option == '0':
submenu(sys_info_list[t_index])
elif option == '':
continue
else:
print('输入有误,请重新选择.')
def query_student(teacher_dict:dict):
print('*' + 'Querying student'.center(51, ' ') + '*')
print('*' + ' ' * 51 + '*')
print('*' + '1.Query by student name'.center(51, ' ') + '*')
print('*' + '2.Query by student id'.center(51, ' ') + '*')
print('*' + '3.Query by age'.center(51, ' ') + '*')
while True:
option = input('请输入选项:')
if option == '1':
query_student_name(teacher_dict)
elif option == '2':
query_student_id(teacher_dict)
elif option == '':
continue
else:
print('输入有误,请重新选择.')
def query_student_name(teacher_dict:dict):
print('*' + 'Querying by name'.center(51, ' ') + '*')
query_name = input('输入需要查询的学生姓名:')
count = 0
for n in range(len(teacher_dict['students'])):
if query_name in teacher_dict['students'][n]['stuname']:
count += 1
print(teacher_dict['students'][n])
print('总共为您找到%d条记录\n' % count)
print('选项:继续查询1/返回上一级0')
while True:
option = input('请输入选项:')
if option == '1':
query_student(teacher_dict)
elif option == '0':
submenu(teacher_dict)
elif option == '':
continue
else:
print('输入有误,请重新选择.')
def query_student_id(teacher_dict:dict):
print('*' + 'Querying by id'.center(51, ' ') + '*')
query_id = input('输入需要查询的学生学号:')
count = 0
for n in range(len(teacher_dict['students'])):
if query_id == teacher_dict['students'][n]['stuid']:
count += 1
print(teacher_dict['students'][n])
print('总共为您找到%d条记录\n' % count)
print('选项:继续查询1/返回上一级0')
while True:
option = input('请输入选项:')
if option == '1':
query_student(teacher_dict)
elif option == '0':
submenu(teacher_dict)
else:
print('输入有误,请重新选择.')
if __name__ == '__main__':
menu()
注:对部分信息的输入没有进行判断