存在问题:功能测试完善,未连接数据库操作,对角色的权限尚未解决,代码不够规范。
main:
import json
all_students = []
while True:
print('================================')
print('1.❤登录')
print('2.❤注册')
print('3.❤返回')
print('================================')
log1 = input('请选择:')
if log1 == '2':
function.register()
elif log1 == '1':
with open('./users.json', 'r', encoding='utf-8') as users:
usernameslist1 = json.load(users)
username2 = input('请输入用户名(6位以上):')
password2 = input('请输入密码(6-8位):')
if function.login(usernameslist1,username2,password2):
while True:
function.interface1(username2)
contrl1 = input('请选择(1-5):')
if contrl1 == '1':
with open('%s.json' % username2, 'r', encoding='utf-8') as students:
dict1 = json.load(students)
lastnum = int(dict1['date'][-1]['num'][-1])
function.wg_input(username2,lastnum)
elif contrl1 == '2':
while True:
print('================================')
print('1.❤查看所有学生')
print('2.❤按姓名查找')
print('3.❤按学号查找')
print('4.❤返回')
sel = input('请选择(1-4):')
print('================================')
function.wg_sel(username2, sel)
if sel == '4':
break
elif contrl1 == '3':
while True:
print('================================')
print('1.❤按姓名查找修改')
print('2.❤按学号查找修改')
print('3.❤返回')
upda = input('请选择:(1-3):')
print('================================')
function.wg_update(username2, upda)
if upda == '3':
break
elif contrl1 == '4':
while True:
print('================================')
print('1.❤按学号查找删除')
print('2.❤按姓名查找')
print('3.❤返回')
del1 = input('请选择(1-3):')
print('================================')
function.wg_del(username2, del1)
if del1 == '3':
break
elif contrl1 == '5':
break
else:
print('输入错误,请重新输入:')
continue
else:
print('账号密码有误,请重新输入')
elif log1 =='3':
break
else:
print('输入错误!请重新输入!')
continue
function:
import json
def interface1(username):
print('================================')
print('❀❀欢迎您:%s' % username)
print('1.❤添加学生')
print('2.❤查看学生')
print('3.❤修改学生信息')
print('4.❤删除学生')
print('5.❤返回')
print('================================')
def wg_input(username2, lastnum):
def get_num(lastnum):
while True:
yield '1%03.d' % (lastnum + 1)
num_gen = get_num(lastnum)
while True:
print('================================')
addname = input('请输入姓名:')
addage = int(input('请输入年龄:'))
addscore = int(input('请输入成绩:'))
addtel = input('请输入电话:')
with open('%s.json' % username2, 'r', encoding='utf-8') as students:
dict1 = json.load(students)
dict1['date'].append(
{'name': addname, 'num': next(num_gen), 'age': addage, 'score': addscore, 'tel': addtel})
with open('%s.json' % username2, 'w', encoding='utf-8') as students:
json.dump(dict1, students)
print('添加成功')
print('1.❤继续添加')
print('2.❤退出')
contrl2 = input('请选择(1-2):')
print('================================')
if contrl2 == '1':
continue
else:
break
def wg_sel(username2, sel):
with open('%s.json' % username2, 'r', encoding='utf-8') as students:
dict2 = json.load(students)
if sel == '1':
for item1 in dict2['date']:
print(item1)
elif sel == '2':
name1 = input('请输入您要查询的姓名:')
i = 0
list1 = []
for i in dict2['date']:
if name1 == i["name"]:
list1.append(i)
if len(list1) == 0:
print('没有找到该记录!!')
else:
for n in list1:
print(n)
elif sel == '3':
list2 = []
num = input('请输入您要查询的学号:')
for i in dict2["name"]:
if num == i['num']:
list2.append(i)
if len(list2) == 0:
print('没有找到该记录!!')
else:
for n in list2:
print(n)
def wg_update(username2, upda):
with open('%s.json' % username2, 'r', encoding='utf-8') as students:
dict3 = json.load(students)
if upda == '1':
upname = input('请输入您需要修改的姓名:')
list1 = []
for i in dict3['date']:
if i['name'] == upname:
list1.append(i)
if len(list1) > 0:
for n in range(len(list1)):
print(n, list1[n])
sure = int(input('请选择你所需要修改的序号:'))
for m in dict3['date']:
if list1[sure] == m:
print('================================')
print('1.❤修改姓名')
print('2.❤修改学号')
print('3.❤修改年龄')
print('4.❤修改成绩')
print('5.❤修改电话')
print('6.❤返回')
print('================================')
upname1 = input('请输入您需要修改的信息:')
if upname1 == '1':
upname2 = input('请输入修改后的姓名:')
m['name'] = upname2
print('修改成功')
elif upname1 == '2':
upnum2 = input('请输入修改后的学号:')
m['num'] = upnum2
print('修改成功')
elif upname1 == '3':
upage2 = input('请输入修改后的年龄:')
m['age'] = upage2
print('修改成功')
elif upname1 == '4':
upscore2 = input('请输入修改后的成绩:')
m['score'] = upscore2
print('修改成功')
elif upname1 == '5':
uptel2 = input('请输入修改后的电话:')
m['tel'] = uptel2
print('修改成功')
elif upname1 == '6':
break
else:
print('输入错误!')
with open('%s.json' % username2, 'w', encoding='utf-8') as new_students:
json.dump(dict3, new_students)
else:
print('没有找到该记录!')
if upda == '2':
upnum = input('请输入您需要修改的学号:')
for i in dict3['date']:
if i['num'] == upnum:
print('================================')
print('1.❤修改姓名')
print('2.❤修改学号')
print('3.❤修改年龄')
print('4.❤修改电话')
print('5.❤修改成绩')
print('6.❤返回')
upname1 = input('请输入您需要修改的信息:')
print('================================')
if upname1 == '1':
upname2 = input('请输入修改后的姓名:')
i['name'] = upname2
print('修改成功')
elif upname1 == '2':
upnum2 = input('请输入修改后的学号:')
i['num'] = upnum2
print('修改成功')
elif upname1 == '3':
upage2 = input('请输入修改后的年龄:')
i['age'] = upage2
print('修改成功')
elif upname1 == '4':
upscore2 = input('请输入修改后的成绩:')
i['score'] = upscore2
print('修改成功')
elif upname1 == '5':
uptel2 = input('请输入修改后的电话:')
i['tel'] = uptel2
print('修改成功')
elif upname1 == '6':
break
with open('%s.json' % username2, 'w', encoding='utf-8') as new_students:
json.dump(dict3, new_students)
def wg_del(username2, del1):
with open('%s.json' % username2, 'r', encoding='utf-8') as students:
dict3 = json.load(students)
if del1 == '1':
del2 = input('请输入您需要删除的学号:')
list2 = []
for i in dict3['date']:
if del2 == i['num']:
list2.append(i)
if len(list2) == 0:
print('没有找到该记录!!')
else:
print('确定删除吗?')
print('1.确定')
print('2.返回')
sure = input('请选择:')
if sure == '1':
for i in dict3['date']:
if del2 == i['num']:
dict3['date'].remove(i)
print('删除成功!')
print(dict3['date'])
with open('%s.json' % username2, 'w', encoding='utf-8') as new_students:
json.dump(dict3, new_students)
if del1 == '2':
name1 = input('请输入您要查询的姓名:')
i = 0
list1 = []
while i in range(len(dict3['date'])):
if dict3['date'][i]['name'] == name1:
list1.append(dict3['date'][i])
i += 1
if len(list1) == 0:
print('没有找到该记录!!')
else:
for n in range(len(list1)):
print(n, list1[n])
sure = int(input('请选择你所需要删除的序号:'))
dict3['date'].remove(list1[sure])
with open('%s.json' % username2, 'w', encoding='utf-8') as new_students:
json.dump(dict3, new_students)
print('删除成功!')
def register():
while True:
username = input('请输入用户名:(6位以上)')
if 6 <= len(username):
while True:
password = input('请输入密码(6-8位):')
if 6 <= len(password) <= 8:
with open('./users.json', 'r', encoding='utf-8') as users:
usernames = json.load(users)
usernames.append({'username': username, 'password': password})
with open('./users.json', 'w', encoding='utf-8') as users:
json.dump(usernames, users)
with open('./%s.json' % username, 'w', encoding='utf-8') as userdate:
dict1 = {'user': username, 'date': []}
json.dump(dict1, userdate)
print('注册成功!')
break
else:
print('输入有误!请重新输入!')
continue
break
else:
print('输入有误!请重新输入!')
continue
def login(usernameslist1: list, username2, password2):
for n in usernameslist1:
if n['username'] == username2 and password2 == n['password']:
return True
elif n == username2 and password2 != usernameslist1[n]:
print('密码有误,请重新登录!')
return False