多用户注册&登录函数封装版
要求:
1.基于文件实现用户注册及登录功能
2.多用户模式,注册登录功能可循环执行
3.将功能封装成函数
def login():
"""用于用户登录的函数"""
print('开始登录'.center(30, '*'))
# 登录功能
login_name = input('请输入用户名>>>:').strip()
login_pwd = input('请输入密码>>>:').strip()
with open(r'info.txt', 'r', encoding='utf8') as user_read:
for line in user_read:
line = line.strip('\n')
if line.split('|')[0] == login_name and line.split('|')[1] == login_pwd:
print('登录成功')
return
else:
print('用户名或密码错误')
return 1
def register():
"""用于用户注册的函数"""
# 注册功能
print('开始注册'.center(30, '*'))
username = input('请输入用户名>>>:').strip()
pwd = input('请输入密码>>>:').strip()
# 判断用户是否已注册
with open(r'info.txt', 'r', encoding='utf8') as if_exist:
for line in if_exist:
if line.split('|')[0] == username:
print('用户已注册')
break
else:
with open(r'info.txt', 'a', encoding='utf8') as user_write:
user_write.write('{}|{}\n'.format(username, pwd))
print('用户:{}注册成功'.format(username))
return
return 1
# 构建功能列表
func_dict = {'1': ['注册', register], '2': ['登录', login]}
while True:
for i in range(1, len(func_dict) + 1):
print(i, func_dict.get(str(i))[0])
choice = input('请输入序号以选择功能(q/Q退出)>>>:').strip()
if choice.upper() == 'Q':
break
while choice.isdigit() and choice in func_dict:
res = func_dict[choice][1]()
if res:
continue
else:
break
else:
print('序号输入有误,请重新输入')
continue