1.01-mainpage.py
import file_manager
# 全局变量
file_name = 'user_info.json' # 保存所有用户的信息的文件
key_user_name = 'user_name'
key_password = 'password'
# ==========================注册=================
"""
为了下次打开系统的时候能够正常登录,注册成功的信息需要保存。保存用户名和密码
一个系统可以注册多个用户,可以用列表保存多个用户。通过字典来保存每个用户名和密码
[
{'user_name':'用户名','password':'密码'}
]
保存到userinfo.json中
"""
def is_register(user_name):
"""
判断指定用户名是否已经注册过
:param user_name:
:return:
"""
all_user = file_manager.read_json_file(file_name)
if not all_user:
return False
for user in all_user:
if user[key_user_name] == user_name:
return True
return False
def get_all_user():
"""
获取所有的用户
:return:
"""
all_user = file_manager.read_json_file(file_name)
if all_user:
return all_user
return []
def register():
# 1.输入用户名
while True:
user_name = input('请输入一个用户名(3-10位):')
# 判断是否符合格式要求
if not 3 <= len(user_name) <= 10:
print('输入有误,请重新输入!')
continue
# 判断是否已经注册过
if is_register(user_name):
print('%s,已经注册过,请重新输入!'% user_name)
continue
print('用户名可用!')
break
# 2.输入密码
while True:
password = input('请输入密码(6-16):')
if not 6 <= len(password) <= 16 :
print('输入密码有误,请重新输入!')
continue
re_password = input('确认密码:')
if password != re_password:
print('和第一次输入的密码不一样,请重新输入!')
continue
break
#3. 保存用户名和密码
all_user = get_all_user()
all_user.append({key_user_name:user_name,key_password:password})
re = file_manager.write_json_file(file_name,all_user)
if re:
print('注册成功!')
else:
print('注册失败!')
# =====================登录=====================
def login():
"""
登录
:return:
"""
user_name = input('请输入用户名:')
password = input('请输入密码:')
#1.看输入的用户名是否已经注册过
all_user = get_all_user()
for user in all_user:
if user[key_user_name] == user_name:
if user[key_password] == password:
print('登录成功!')
return user_name
else:
print('密码错误,登录失败!')
return
print('没有注册,登录失败!')
return None
# =========================主页==================
import student_system
def show_main_page():
while True:
print(file_manager.read_text_file('login.txt'))
value = input('请选择(1-3):')
# 1.退出
if value == '3':
break
#2.注册
elif value == '2':
register()
elif value == '1':
user_name = login()
if user_name:
# 如果登录成功就进入管理系统
student_system.user_name = user_name
student_system.main_page()
else:
print('输入有误,重新选择!')
# 1.显示登录界面(主界面)
if __name__ == '__main__':
#1.显示登录界面(主界面)
show_main_page()
2.file_manager.py
import json
def write_json_file(file_name,content):
"""
将内容写入json文件中
:param file_name: 文件名
:param content: 要写的内容
:return: 是否写入成功
"""
try:
with open('./files/'+file_name,'w',encoding='utf-8') as f:
json.dump(content,f,ensure_ascii=False,indent=2)
return True
except:
return False
def read_json_file(file_name):
"""
读取json文件的内容
:param file_name: 文件名
:return: 文件内容
"""
try:
with open('./files/'+file_name,encoding='utf-8') as f:
return json.load(f)
except:
return None
def read_text_file(file_name):
"""
获取文本文件的内容
:param file_name:文件名
:return:文件中的内容
"""
try:
with open('./files/'+file_name,encoding='utf-8') as f:
return f.read()
except FileNotFoundError:
print('文件不存在')
return None
3.student_system.py
import file_manager
user_name = ''
# =========================添加学生=======
"""
一个账户对应管理不同的学生---不同的用户对应不同的json文件
json文件中的格式:
{
'name':'',
'number':个数,
'all_students':[
{'name':,'age':,'tel':,'id':}
]
}
"""
key_number = 'number'
key_all_students = 'all_students'
key_name = 'name'
key_age = 'age'
key_tel = 'tel'
key_id = 'id'
def get_system_info():
"""获取系统文件内容"""
system_info = file_manager.read_json_file(user_name+'.json')
if system_info:
return system_info
return {}
def creat_id():
"""产生学号"""
system_info = get_system_info()
number = system_info.get(key_number,0)
number += 1
id = 'stu' + str(number).rjust(4,'0')
return id,number
def add_student():
while True:
# 1.输入信息
name = input('姓名:')
age = input('年龄:')
tel = input('电话:')
# 2.产生id
id, number = creat_id()
# 3.创建学生
stu = {key_name: name, key_age: age, key_tel: tel, key_id: id}
# 4.保存学生信息
system_info = get_system_info()
all_student = system_info.get(key_all_students,[])
all_student.append(stu)
# 5.保存到文件中
system_info[key_all_students] = all_student
system_info[key_number] = number
re = file_manager.write_json_file(user_name + '.json', system_info)
if re:
print('添加成功!')
else:
print('添加失败')
print('1.继续添加')
print('2.返回')
value = input('请选择(1/2):')
if value == '1':
continue
else:
break
#===================查找学生==============
def find_student():
all_students = get_system_info().get(key_all_students,[])
if not all_students:
print('目前还没有学生!')
return
print('1.查看所有的学生')
print('2.根据姓名查找学生')
print('3.根据学号查找学生')
value = input('请选择(1-3):')
if value == '1':
for stu in all_students:
print('姓名:%s 学号:%s 年龄:%s 电话:%s'%(stu[key_name],stu[key_id],stu[key_age],stu[key_tel]))
if value == '2':
name = input('姓名:')
for stu in all_students:
if stu[key_name] == name:
print('姓名:%s 学号:%s 年龄:%s 电话:%s' % (stu[key_name], stu[key_id], stu[key_age], stu[key_tel]))
break
else:
print('没有该学生!!')
if value == '3':
id = input('学号:')
for stu in all_students:
if stu[key_id] == id:
print('姓名:%s 学号:%s 年龄:%s 电话:%s' % (stu[key_name], stu[key_id], stu[key_age], stu[key_tel]))
break
else:
print('没有该学生!')
#=================================删除学生============================
def delete_student():
print('1.按姓名删除学生')
print('2.按学号删除学生')
system_info = get_system_info()
all_students = get_system_info().get(key_all_students,[])
if not all_students:
print('目前还没有学生!')
return
value = input('请选择(1-2):')
if value == '1':
name = input('请输入你要删除学生的姓名:')
for stu in all_students[:]:
if stu[key_name] == name:
all_students.remove(stu)
print('该学生已删除')
break
system_info[key_all_students] = all_students
file_manager.write_json_file(user_name+'.json',system_info)
elif value == '2':
id = input('请输入你要删除学生的学号:')
for stu in all_students[:]:
if stu[key_id] == id:
all_students.remove(stu)
print('该学生已删除')
break
system_info[key_all_students] = all_students
file_manager.write_json_file(user_name+'.json',system_info)
def revise_student():
print('1.按姓名修改学生')
print('2.按学号修改学生')
system_info = get_system_info()
all_students = get_system_info().get(key_all_students, [])
if not all_students:
print('目前还没有学生!')
return
value = input('请选择(1-2):')
if value == '1':
name = input('请输入你要修改学生的姓名:')
for stu in all_students[:]:
if stu[key_name] == name:
age = input('年龄:')
tel = input('电话:')
stu[key_age] = age
stu[key_tel] = tel
print('修改学生信息成功')
system_info[key_all_students] = all_students
file_manager.write_json_file(user_name + '.json', system_info)
if value == '2':
id = input('请输入你要修改学生的学号:')
for stu in all_students[:]:
if stu[key_id] == id:
name = input('名字:')
age = input('年龄:')
tel = input('电话:')
stu[key_name] = name
stu[key_age] = age
stu[key_tel] = tel
print('修改学生信息成功')
system_info[key_all_students] = all_students
file_manager.write_json_file(user_name + '.json', system_info)
# ==========================主页=========
def main_page():
while True:
print(file_manager.read_text_file('system.txt'))
value = input('请选择(1-5):')
if value == '5':
break
elif value == '1':
add_student()
elif value == '2':
find_student()
elif value == '3':
delete_student()
elif value == '4':
revise_student()
else:
print('输入有误,重新输入')
用户注册与登录
===============================
== == == == == == == ==
===========学生管理系统=========
1.登 录
2.注 册
3.退 出
== == == == == == == ==
===============================
请选择(1-3):2
请输入一个用户名(3-10位):hyc
用户名可用!
请输入密码(6-16):123456
确认密码:123456
注册成功!
===============================
== == == == == == == ==
===========学生管理系统=========
1.登 录
2.注 册
3.退 出
== == == == == == == ==
===============================
请选择(1-3):1
请输入用户名:hyc
请输入密码:123456
登录成功!
添加学生与查看学生
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
welcome aaa inter student manager system
+ +
+ 1. Add Student +
+ 2. Find Student +
+ 3. Delete Student +
+ 4. Revise StudentInfo +
+ 5. Back +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++
请选择(1-5):1
姓名:江秀成
年龄:23
电话:123456
添加成功!
1.继续添加
2.返回
请选择(1/2):2
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
welcome aaa inter student manager system
+ +
+ 1. Add Student +
+ 2. Find Student +
+ 3. Delete Student +
+ 4. Revise StudentInfo +
+ 5. Back +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++
请选择(1-5):2
1.查看所有的学生
2.根据姓名查找学生
3.根据学号查找学生
请选择(1-3):1
姓名:江秀成 学号:stu0001 年龄:23 电话:123456
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
welcome aaa inter student manager system
+ +
+ 1. Add Student +
+ 2. Find Student +
+ 3. Delete Student +
+ 4. Revise StudentInfo +
+ 5. Back +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++
请选择(1-5):1
姓名:袁学斌
年龄:24
电话:345
添加成功!
1.继续添加
2.返回
请选择(1/2):1
姓名:王渠江
年龄:78
电话:2345
添加成功!
1.继续添加
2.返回
请选择(1/2):2
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
welcome aaa inter student manager system
+ +
+ 1. Add Student +
+ 2. Find Student +
+ 3. Delete Student +
+ 4. Revise StudentInfo +
+ 5. Back +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++
请选择(1-5):2
1.查看所有的学生
2.根据姓名查找学生
3.根据学号查找学生
请选择(1-3):1
姓名:江秀成 学号:stu0001 年龄:23 电话:123456
姓名:袁学斌 学号:stu0002 年龄:24 电话:345
姓名:王渠江 学号:stu0003 年龄:78 电话:2345
删除学生
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
welcome aaa inter student manager system
+ +
+ 1. Add Student +
+ 2. Find Student +
+ 3. Delete Student +
+ 4. Revise StudentInfo +
+ 5. Back +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++
请选择(1-5):3
1.按姓名删除学生
2.按学号删除学生
请选择(1-2):1
请输入你要删除学生的姓名:江秀成
该学生已删除
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
welcome aaa inter student manager system
+ +
+ 1. Add Student +
+ 2. Find Student +
+ 3. Delete Student +
+ 4. Revise StudentInfo +
+ 5. Back +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++
请选择(1-5):2
1.查看所有的学生
2.根据姓名查找学生
3.根据学号查找学生
请选择(1-3):1
姓名:袁学斌 学号:stu0002 年龄:24 电话:345
姓名:王渠江 学号:stu0003 年龄:78 电话:2345
修改学生
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
welcome aaa inter student manager system
+ +
+ 1. Add Student +
+ 2. Find Student +
+ 3. Delete Student +
+ 4. Revise StudentInfo +
+ 5. Back +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++
请选择(1-5):4
1.按姓名修改学生
2.按学号修改学生
请选择(1-2):1
请输入你要修改学生的姓名:袁学斌
年龄:56
电话:678
修改学生信息成功
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
welcome aaa inter student manager system
+ +
+ 1. Add Student +
+ 2. Find Student +
+ 3. Delete Student +
+ 4. Revise StudentInfo +
+ 5. Back +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++
请选择(1-5):2
1.查看所有的学生
2.根据姓名查找学生
3.根据学号查找学生
请选择(1-3):1
姓名:袁学斌 学号:stu0002 年龄:56 电话:678
姓名:王渠江 学号:stu0003 年龄:78 电话:2345