...................................................................................
import json
with open('users.json', 'w') as f_users:
json.dump([], f_users)
with open('student_system.json', 'w') as f_student:
json.dump([], f_student)
......................................................................................
print('学生管理系统')
print('===========================')
import json
def zhanghao():
username = input('请输入用户名:')
password = input('请输入密码:')
return username,password
def add_student():
stu_id=input('请输入学号:')
name = input('请输入姓名:')
age = int(input('请输入年龄:'))
tel = int(input('请输入电话:'))
return stu_id,name,age,tel
def xuanze():
option5=int(input('请输入选项:'))
return option5
def option2():
print('1:继续', '2:返回上一级', sep='\n')
x=input('请输入选项:')
while True:
o = int(x)
if o==1 or o==2:
return o
else:
x = input('输入错误,请重新输入选项:')
def fenge():
print('===============================')
=======================登录==============================
with open('users.json', 'r') as f_users:
users=json.load(f_users)
while True:
print('1:注册账号', '2:登录账号', sep='\n')
xuanxiang=xuanze()
if xuanxiang==1:
username,password = zhanghao()
user = {'username': username, 'password': password}
users.append(user)
with open('users.json','w') as f_users:
json.dump(users,f_users)
with open('student_system.json','r') as fr_student:
student_system=json.load(fr_student)
student_system.append([])
with open('student_system.json', 'w') as f_student:
json.dump(student_system,f_student)
print('注册成功,返回上一级')
fenge()
continue
elif xuanxiang==2:
username, password = zhanghao()
user_index = -1
count = 0
with open('users.json') as fr_users:
users=json.load(fr_users)
for x in users[:]:
user_index += 1
if x['username']==username:
count+=1
while True:
if x['password']==password:
print('登陆成功')
fenge()
break
else:
print('密码错误,请重新输入',)
fenge()
password = input('请输入密码:')
if password==1:
break
break
if count==0:
print('用户不存在,请重新输入')
continue
else:
print('输入错误,请重新输入')
continue
break
=========================操作===============================
with open('student_system.json', 'r') as f_student:
student_system =json.load(f_student)
userx=student_system[user_index]
while True:
print('1:添加学生', '2:修改学生', '3:删除学生','4:查看所有学生' ,'5:退出', sep='\n')
fenge()
a=xuanze()
if a<=5:
option =a
else:
print('输入错误,请重新输入')
continue
while option<5:
# ==============================================
if option==1: #添加学生
stu_id,name,age,tel=add_student()
fenge()
student={'stu_id':stu_id,'name':name,'age':age,'Tel':tel}
userx.append(student)
with open('student_system.json', 'w') as f_student:
json.dump(student_system,f_student)
print('添加完成')
fenge()
option1 = option2()
fenge()
if option1 == 1:
continue
elif option1 == 2:
break
else:
print('输入错误,请重新输入')
# =================================================
elif option==2:#修改学生,输入姓名,查找学生进行修改
with open('student_system.json', 'r') as fr_student:
student_system=json.load(fr_student)
name = input('请输入姓名:')
fenge()
index=-1
for x in userx[:]:
index += 1
if x['name']==name:
print(index,x,sep=':')
fenge()
stu_option=int(input('请输入选项:'))
age = int(input('请输入年龄:'))
tel = int(input('请输入电话:'))
userx[stu_option]['age']=age
userx[stu_option]['tel']=tel
with open('student_system.json', 'w') as f_student:
json.dump(student_system,f_student)
print('修改完成',userx[stu_option])
fenge()
option1 = option2()
fenge()
if option1 == 1:
continue
else:
break
# =======================================
elif option == 3: #删除学生
with open('student_system.json', 'r') as fr_student:
student_system=json.load(fr_student)
name = input('请输入姓名:')
fenge()
index = -1
for x in userx[:]:
index += 1
if x['name'] == name:
print(index, x, sep=':')
fenge()
stu_option = int(input('请输入选项:'))
del userx[stu_option]
with open('student_system.json', 'w') as f_student:
json.dump(student_system,f_student)
print('删除完成')
fenge()
option1 = option2()
fenge()
if option1 == 1:
continue
else:
break
elif option==4: #查看所有学生
with open('student_system.json', 'r') as fr_student:
student_system=json.load(fr_student)
for stu in userx[:]:
print(stu)
fenge()
option1 = option2()
fenge()
if option1 == 1:
continue
else:
break
if option==5:
break
print('退出系统')
网友评论