设计管理系统
def show_welcome():
welcome = """
========================================
**欢迎来到XX管理系统**
1.登 录
2.注 册
3.退 出
========================================
"""
print(welcome)
def new_file_dict():
f = open(data_loc, 'w', encoding='utf-8')
f.write('{}')
f.close()
def check_data_file(path: str, contents='{}'):
try:
with open(path, 'r+', encoding='utf-8') as f:
open_flag = 0
result = f.read()
if not len(result):
f.write(contents)
f.seek(0)
result = f.read()
result = eval(result)
if type(result) != dict:
print('文件里数据不是字典')
raise TypeError
except FileNotFoundError:
open_flag = 1
new_file_dict()
finally:
if open_flag == 1:
print('没有该文件,但已成功创建')
def check_acc(account_name):
try:
with open(data_loc, 'r+', encoding='utf-8') as f:
open_flag = 0
result = eval(f.read())
value = result.get(account_name, '账号不存在')
if value == '账号不存在':
return False, result
else:
print('该账号已存在')
return True, result
except:
open_flag = 1
finally:
if open_flag == 1:
print('文件打开时出错!')
def register():
account_name = input('请输入账号(3-6位): ')
pass_word = input('请输入账号(6-12位): ')
if not (3 <= len(account_name) <= 6 and 6 <= len(pass_word) <= 12):
print('账号或密码长度不对')
return
exist, result = check_acc(account_name)
if exist:
return
with open(data_loc, 'wt', encoding='utf-8') as f:
result.update({account_name: pass_word})
f.write(str(result))
print('注册成功!')
return
def log_in():
account_name = input('请输入账号: ')
pass_word = input('请输入密码: ')
try:
f = open(data_loc)
open_flag = 0
result = eval(f.read())
value = result.get(account_name, '账号不存在')
f.close()
if value == '账号不存在':
print('账号不存在')
return
elif value == pass_word:
print('登陆成功')
return
except:
open_flag = 1
finally:
if open_flag == 1:
print('文件打开时出错!')
def exit_sys():
print('您退出了系统!')
exit()
def operator_manage_sys():
while True:
show_welcome()
operator = input('请选择1-3: ')
if operator == '1':
log_in()
elif operator == '2':
register()
elif operator == '3':
exit_sys()
else:
print('请选择正确操作')
if __name__ == '__main__':
data_loc = 'text.txt'
check_data_file(data_loc)
operator_manage_sys()