管理系统作业
"""
创建者:卢俊杰
创建时间:2022/12/28,17:09
"""
import os
def initialize():
if os.path.isfile("file\students.txt"):
pass
else:
with open(r'file\students.txt', 'w') as fw:
fw.write(str([]))
def find_students(students: list[dict], name):
for num, student in enumerate(students):
if student['name'] == name:
return num, True
else:
return -1, False
def get_students():
fr = open(r'file\students.txt', 'r')
students: list = eval(fr.read())
fr.close()
return students
def login():
name = input('输入账户:')
passwords = input('请输入密码')
students = get_students()
nums, is_exist = find_students(students, name)
if is_exist:
if students[nums]['password'] == passwords:
print('密码正确')
else:
print('密码错误')
else:
print('用户不存在')
def enroll():
name = input('请输入账户名:')
password = input('请输入密码:')
student_info = {
'name': name,
'password': password
}
nums, is_exist = find_students(get_students(), name)
if is_exist:
print('用户已经存在')
else:
new_student_info: list = get_students()
new_student_info.append(student_info)
with open(r'file\students.txt', 'w') as fw:
fw.write(str(new_student_info))
print('创建成功')
def hello():
print('=======================================================')
print(' ***欢迎到学生管理系统** ')
print(' 1, 登录 ')
print(' 2, 注册 ')
print(' 3, 退出 ')
print('=======================================================')
initialize()
while True:
hello()
options = input('请输入选项(1-3)')
match options:
case '1':
login()
case '2':
enroll()
case '3':
exit(0)
case _:
print('输入错误')