一、主界面
def home(str1 = '游客'):
print('★★★★★★★★★★★★★★★★★★★★')
print(' 欢迎%s使用学生管理系统 ' % str1)
print('+ +')
print('+ 1.regist +')
print('+ +')
print('+ 2.login +')
print('+ +')
print('+ 3.quest +')
print('+ +')
print('★★★★★★★★★★★★★★★★★★★★')
return
if __name__ == '__main__':
pass
二、
import json
key_name = 'user_name'
key_pwd = 'user_pwd'
key_stu = 'student_info'
student_info =[]
def user_regist():
# 账号
try:
with open('./user.json', 'r', encoding='utf-8') as k:
content = json.load(k)
except FileNotFoundError:
content = []
# 取出文件中的数据,判断用户名是否被注册过
while True:
user_name = input('请输入用户名:')
for index in range(len(content)):
if content[index][key_name] == user_name:
print('用户名已经存在')
return
else:
continue
break
print('用户名可以使用')
user_pwd = input('请输入密码:')
user_pwd1 = input('请再次输入密码:')
# 判断两次密码是否相同
if user_pwd != user_pwd1:
print('两次输入的密码不一样')
return
else:
user = {key_name: user_name, key_pwd: user_pwd, key_stu: student_info}
content.append(user)
with open('./user.json', 'w', encoding='utf-8') as k:
json.dump(content, k)
return None
if __name__ == '__main__':
user_regist()
三、
import json
key_name = 'user_name'
key_pwd = 'user_pwd'
key_stu = 'student_info'
stu_key_name = 'stu_name'
stu_key_tel = 'stu_phone'
stu_key_age = 'stu_age'
student_info =[]
userinfo = []
def user_login():
# 打开文件,判断文件是否存在
try:
with open('./user.json', 'r', encoding='utf-8') as k:
content = json.load(k)
except FileNotFoundError:
print('没有用户')
content = []
# 判断用户是否存在
while True:
user_name = input('请输入用户名:')
user_pwd = input('请输入密码:')
for index in range(0,len(content)):
if content[index][key_name] == user_name:
userinfo = content[index]
break
else:
print('用户不存在')
continue
# 获得你登录的用户的学生信息
# for index in range(len(content)):
# if content[index][key_name] == user_name and content[index][key_pwd] == user_pwd:
# print('登录成功')
# break
if userinfo[key_pwd] == user_pwd:
print('登录成功')
break
else:
print('密码错误')
continue
student_info = content[index][key_stu]
# 学生管理功能
while True:
print('1、添加学生')
print('2、删除学生')
print('3、查看学生')
print('4、返回上一层')
n = input('请选择:')
if '0' < n < '5':
if n == '1': # 添加
stu_name = input('请输入学生姓名:')
stu_tel = input('请输入学生电话:')
stu_age = input('请输入学生年龄:')
student = {stu_key_name: stu_name, stu_key_tel: stu_tel, stu_key_age: stu_age}
student_info.append(student)
with open('./user.json', 'w', encoding='utf-8') as u:
json.dump(content, u)
print('添加成功')
if n == '2': # 删除
m = True
while m:
# 如果没有存入学生
if not len(student_info):
print('没有学生')
break
get_name = input('请输入要删除的学生的姓名:')
flag = True
a = []
copy_name = []
# 通过名字查找学生
for i in range(0, len(student_info)):
if get_name == student_info[i][stu_key_name]:
a.append(i)
copy_name.append(student_info[i])
# 如果没有重名,直接删除
if len(copy_name) == 1:
for i in range(0, len(student_info)):
if get_name == student_info[i][stu_key_name]:
del student_info[i]
break
flag = False
with open('./user.json', 'w', encoding='utf-8') as u:
json.dump(content, u)
print('删除成功!')
# 如果有重名选择删除的人
elif len(copy_name) > 1:
for i in range(0, len(copy_name)):
print(copy_name[i])
del_name = int(input('你想删除第几个?')) - 1
del student_info[a[del_name]]
flag = False
with open('./user.json', 'w', encoding='utf-8') as u:
json.dump(content, u)
print('删除成功!')
if flag:
print('该学生不存在')
m = int(input('输入1继续删除,输入0返回上级:'))
if n == '3': # 查看
print(student_info)
if n == '4':
print('返回上一层')
return
else:
print('你的输入有误!')
continue
if __name__ == '__main__':
pass
四、测试