登陆界面
- 保存注册账号密码
- 不能重复注册
- 账号密码错误不能登陆
主要函数
choose_face()
def choose_face():
choose_content = '''
==========homework==========
🐍
1.登陆账号
2.修改密码
3.注册账号
4.退出
============================
'''
print(choose_content)
while True:
fn_choose = input('请选择1~4:\n')
if fn_choose == '1':
log_in()
if fn_choose == '2':
change_password()
if fn_choose == '3':
log_up()
if fn_choose == '4':
print('结束\n')
break
登陆函数
def log_in():
user_name = input('用户名:\n')
pass_word = input('密码:\n')
with open('./registered_users', 'r', encoding='utf-8') as f:
try:
registered_users = eval(f.read())
if registered_users[user_name] == pass_word:
print('登陆成功!')
else:
print('用户名或密码不正确!')
except:
print('用户名或密码不正确!')
修改密码
def change_password():
user_name = input('用户名:\n')
pass_word = input('旧密码:\n')
with open('./registered_users', 'r', encoding='utf-8') as f:
try:
registered_users = eval(f.read())
if registered_users[user_name] == pass_word:
new_password = input('新密码:\n')
if new_password:
with open('./registered_users', 'w', encoding='utf-8') as f:
registered_users[user_name] = new_password
f.write(str(registered_users))
print('密码修改成功!')
else:
print('没有设置新密码!')
except:
print('用户名或密码不正确!')
注册函数
def log_up():
user_name = input('请输入用户名:\n')
if user_name:
with open('./registered_users', 'r', encoding='utf-8') as f:
if not f.read():
with open('./registered_users', 'w', encoding='utf-8') as ff:
ff.write(str({}))
f.seek(0)
registered_users = eval(f.read())
if user_name not in registered_users:
pass_word = input('请设置密码:\n')
if pass_word:
registered_users[user_name] = pass_word
with open('./registered_users', 'w', encoding='utf-8') as f:
f.write(str(registered_users))
print('注册成功!')
else:
print('没有设置密码!')
else:
print('已有用户名,请有点创意!')
else:
print('没有输入用户名!')