登录模块
import fileManager
import studentManagePage
"""
users_and_password.json
此文件保存的是用户名和密码
格式如下:
{'ququ': 123456, 'jojo': 222222}
读内容:
all_users = fileManager.read_json_file('users_and_password.json')
写内容:
fileManager.write_json_file(content, 'users_and_password.json')
"""
file_name = 'users_and_password.json'
def check_password(username, password):
all_users = fileManager.read_json_file(file_name) or {}
if all_users[username] == password:
return True
else:
return False
def has_this_name(username: str):
"""
检查输入的用户名是否在文件中
:param username: 输入的用户名
:return: 如果在返回True,否则返回False
"""
all_users = fileManager.read_json_file(file_name) or {}
if username in all_users:
return True
else:
return False
def login():
username = input('请输入用户名:')
if not has_this_name(username):
print('此用户名没有注册!')
return
else:
password = input('请输入密码:')
if check_password(username, password):
studentManagePage.user_name = username
studentManagePage.show_student_manage_page()
else:
print('密码或用户名有误')
return
def register():
username = input('请输入用户名:')
all_users = fileManager.read_json_file(file_name) or {}
if has_this_name(username):
print('此用户名已注册!')
return
else:
password = input('请输入密码:')
all_users[username] = password
if fileManager.write_json_file(all_users, file_name):
print('注册成功!')
else:
print('注册失败!')
def show_login_page():
print(fileManager.read_text_file('login_page.txt'))
def reg_and_login():
while True:
show_login_page()
input_value = input("请选择(1-3)")
if input_value == '1':
login()
elif input_value == '2':
register()
elif input_value == '3':
return
else:
print('输入有误!')
def main():
reg_and_login()
if __name__ == '__main__':
main()
学生管理模块
import fileManager
user_name = ''
key_stu_name = 'name'
key_stu_age = 'age'
key_stu_tel = 'tel'
key_stu_id = 'id'
key_all_student = 'all_student'
key_max_stu_id = 'max_stu_id'
"""
每个用户都有一个独立的学生信息文件,以 用户名+'.json' 命名
文件里面的内容如下(用字典保存的结构,好处是后期可以扩展)
all_student - 学生信息
current_stu_num - 当前已用的学号
all_student: list
student_system: dict
all_student = [
{'name': , 'age': , 'tel': , 'id': },
{'name': , 'age': , 'tel': , 'id': },
{'name': , 'age': , 'tel': , 'id': },
....
]
student_system = {
'all_student': [
{'name': , 'age': , 'tel': , 'id': },
{'name': , 'age': , 'tel': , 'id': },
{'name': , 'age': , 'tel': , 'id': },
....
],
'max_stu_id': 1
}
"""
def modify_stu(input_id: str) -> dict:
"""
修改学生信息
:param input_id:
:return:
"""
name = input('姓名:')
age = input('年龄:')
tel = input('电话:')
return {key_stu_id: input_id, key_stu_name: name, key_stu_age: age, key_stu_tel: tel}
def unique_stu(same_name_students: list) -> dict:
"""
从同名学生中根据学号选择唯一一名学生,如果没有,或输入错误返回空
:param same_name_students:
:return:
"""
input_num = input('更改或删除学生,请选择对应编号学生(其他键退出):')
if input_num.isdigit():
index = int(input_num)
if index <= len(same_name_students):
return same_name_students[index - 1]
else:
return {}
def del_or_mod():
"""
对搜索出来的学生进入子菜单选项
:return:
"""
while True:
input_value = input('M.修改 D.删除 其他键返回(请选择):')
if input_value == 'M' or input_value == 'm':
return 1
elif input_value == 'D' or input_value == 'd':
input_char = input('是否删除?(按Y确定):')
if input_char == 'Y' or input_char == 'y':
return 2
else:
return 0
def is_content_empty(content):
"""
判断参数里的内容是否为空
:param content:
:return: 五内容返回True
"""
if content:
return False
else:
print('暂无学生信息,请先添加!')
print('========================================')
return True
def show_student_info(stu: dict):
return '学号:%s 姓名:%s 年龄:%s 电话:%s' % (stu[key_stu_id], stu[key_stu_name], stu[key_stu_age], stu[key_stu_tel])
def manage_stu():
print('================管理学生================')
student_system = fileManager.read_json_file(user_name + '.json') or {}
all_student = student_system.get(key_all_student, [])
if is_content_empty(student_system):
print('没有学生信息,请先添加!')
return
if is_content_empty(all_student):
print('没有学生信息,请先添加!')
return
while True:
print('♦1.显示所有学生')
print('♦2.按姓名搜索')
print('♦3.按学号搜索')
print('♦4.返回')
input_value = input('请选择(1-4):')
if input_value == '4':
print('========================================')
return
elif input_value == '1':
for stu in all_student:
print(show_student_info(stu))
elif input_value == '2':
input_name = input('请输入姓名:')
count = 0
same_name_students = []
for stu in all_student:
if stu[key_stu_name] == input_name:
count += 1
print("%d > " % count + show_student_info(stu))
same_name_students.append(stu)
if count == 0:
print('没有此学生信息!')
else:
if count == 1:
result_stu = same_name_students[0]
else:
result_stu = unique_stu(same_name_students)
if result_stu:
choose_func = del_or_mod()
if choose_func == 1:
select_stu_id = result_stu[key_stu_id]
modified_stu = modify_stu(select_stu_id)
all_student.append(modified_stu)
all_student.remove(result_stu)
elif choose_func == 2:
all_student.remove(result_stu)
else:
return
student_system[key_all_student] = all_student
if fileManager.write_json_file(student_system, user_name + '.json'):
return True
else:
return False
elif input_value == '3':
input_id = input('请输入学号:')
for stu in all_student[:]:
if stu[key_stu_id] == input_id:
print(show_student_info(stu))
choose_func = del_or_mod()
if choose_func == 1:
modified_stu = modify_stu(input_id)
all_student.remove(stu)
all_student.append(modified_stu)
print(all_student)
elif choose_func == 2:
all_student.remove(stu)
else:
return
student_system[key_all_student] = all_student
if fileManager.write_json_file(student_system, user_name + '.json'):
print('===========操作成功=============')
else:
print('===========操作失败=============')
break
else:
print('没有此学生信息!')
else:
print('输入有误')
def add_stu():
print('================添加学生================')
while True:
print('♦1.添加')
print('♦2.返回')
input_value = input('请选择(1-2):')
if input_value != '1':
print('========================================')
return
else:
name = input('姓名:')
age = input('年龄:')
tel = input('电话:')
student_system = fileManager.read_json_file(user_name + '.json') or {}
all_student = student_system.get(key_all_student, [])
max_stu_id = student_system.get(key_max_stu_id, 0)
max_stu_id += 1
stu_id = 'Python' + str(max_stu_id).zfill(3)
stu = {key_stu_id: stu_id, key_stu_name: name, key_stu_age: age, key_stu_tel: tel}
all_student.append(stu)
student_system[key_max_stu_id] = max_stu_id
student_system[key_all_student] = all_student
if fileManager.write_json_file(student_system, user_name + '.json'):
print('================添加学生成功!================')
else:
print('================添加学生失败!================')
def show_student_manage_page():
while True:
print(fileManager.read_text_file('studentMenuPage.txt') % user_name)
input_value = input('请选择(1-3)')
if input_value == '1':
add_stu()
elif input_value == '2':
manage_stu()
elif input_value == '3':
return
else:
print('输入有误!')