登录界面
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():
"""
注册
:return:
"""
# 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 None
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':
# print('登录')
user_name = login()
if user_name:
# 如果登录成功就进入管理系统
student_system.user_name = user_name
student_system.main_page()
else:
print('输入有误, 重新选择!')
if __name__ == '__main__':
# 1.显示登录界面(主界面)
show_main_page()
学生系统界面
import file_manager
user_name = ''
'''
一个账号对应管理不同的学生 --- 不同的用户对应不同的json文件
json文件中的格式:
{
'name':'',
'number':个数,
'all_student':[
{'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 main_interface():
return print("""
*****欢迎进入千峰学生管理系统****
* 1.添加学生 *
* 2.删除学生 *
* 3.修改信息 *
* 4.查找信息 *
* 5.退出系统 *
******************************
""")
# 1.添加学生
def add_student():
Students_table = []
while True:
with open('./student.txt', 'r', encoding='utf-8') as f:
content = f.read()
# print(eval(content))
# f = open('./student.txt', 'r', encoding='utf-8')
# f.read(str(student) + '\n')
# f.close()
name = input('请输入学生姓名:')
age = input('请输入学生年龄:')
tel = input('请输入学生电话:')
# 产生学号
id, number = creat_id()
student = {key_name: name, key_age: age, key_tel: tel, key_id: id}
Students_table.append(student)
with open('./student.txt', 'w', encoding='utf-8') as f:
content = f.write(str(Students_table))
# f = open('./student.txt', 'a', encoding='utf-8')
# f.write(str(student) + '\n')
# f.close()
print('添加成功')
print('==============================')
print('1.继续添加')
print('2.返回上一级')
print('==============================')
number2 =int (input('请选择:'))
if number2 == 1:
continue
else:
break
# 2.删除学生
def del_student():
while True:
with open('./student.txt', 'r', encoding='utf-8') as f:
Students_table = eval(f.read())
print(Students_table)
name2 = input('请输入需要删除学生的姓名:')
for student in Students_table:
if student['name'] == name2:
Students_table.remove(student)
print(Students_table)
print('==============================')
with open('./student.txt', 'w', encoding='utf-8') as f:
content = f.write(str(Students_table))
print('删除成功')
print('==============================')
print('1.继续删除')
print('2.退出')
print('==============================')
number2 = int(input('请选择:'))
if number2 == 1:
print('==============================')
continue
else:
break
# 3.修改信息
def recompose_student():
while True:
with open('./student.txt', 'r', encoding='utf-8') as f:
Students_table = eval(f.read()) # 从文件中取出的数据赋值个一个列表
print(Students_table)
name_gai = input('请输入要修改的姓名:')
for student in Students_table:
if student['name'] == name_gai:
print('=============================')
print(student)
print('==============================')
print('1.修改姓名')
print('2.修改年龄')
print('3.修改电话')
print('4.修改学号')
print('==============================')
number3 = int(input('请选择:'))
print('==============================')
if number3 == 1:
name3 = input('请输入修改后的姓名:')
print('============================')
student['name'] = name3
elif number3 == 2:
age3 = input('请输入修改后的年龄:')
print('============================')
student['age'] = age3
elif number3 == 3:
tel3 = input('请输入修改后的电话:')
print('============================')
student['tel'] = tel3
else:
num3 = input('请输入修改后的学号:')
print('============================')
student['id'] = num3
print(Students_table)
with open('./student.txt', 'w', encoding='utf-8') as f:
content = f.write(str(Students_table))
print('==============================')
print('修改成功')
print('==============================')
print('1.继续修改')
print('2.退出')
print('==============================')
number3 = int(input('请选择:'))
if number3 == 1:
continue
else:
break
# 4.查找信息
def find_student():
while True:
with open('./student.txt', 'r', encoding='utf-8') as f:
Students_table = eval(f.read()) # 从文件中取出的数据赋值个一个列表
print('1.按姓名查找')
print('2.按学号查找')
print('3.查看全部学生')
print('==============================')
number4 = int(input('请选择:'))
print('==============================')
if number4 == 1:
name4 = input('请输入查找的学生姓名:')
print('==============================')
for student in Students_table:
if student['name'] == name4:
print(student)
print('==============================')
print('1.继续查找')
print('2.退出')
print('==============================')
number4 = int(input('请选择:'))
if number4 == 1:
continue
else:
break
elif number4 == 2:
id4 = input('请输入学生的学号:')
print('==============================')
for student in Students_table:
if student['id'] == id4:
print(student)
print('==============================')
print('1.继续查找')
print('2.退出')
print('==============================')
number4 = int(input('请选择:'))
if number4 == 1:
continue
else:
break
elif number4 == 3:
for student in Students_table:
print(student)
print('==============================')
print('1.继续查找')
print('2.退出')
print('==============================')
number4 = int(input('请选择:'))
if number4 == 1:
continue
else:
break
#==============主页===================
def main_page():
while True:
print(file_manager.read_text_file('system.txt'))
value = input('请选择(1-5):')
# 1.返回
if value == '5':
break
elif value == '1':
add_student()
print('添加学生')
elif value == '2':
del_student()
print('删除学生')
elif value == '3':
recompose_student()
print('修改学生')
elif value == '4':
find_student()
print('查找学生')
else:
print('输入有误,重新输入!')
if __name__ == '__main__':
# main_page()
print({}.get('abc', 0))
网友评论