03-27homework

import json
def get_user_name():
try:
with open('user.json',encoding='utf-8') as f:
all_users = json.load(f)
return all_users
except:
return {}
def login():
user_name = input('请输入账号:')
keyword = input('请输入密码:')
all_users = get_user_name()
if not user_name in all_users:
print('登陆失败!账号不存在')
else:
if not keyword == all_users[user_name]:
print('登陆失败,密码错误')
else:
print('登陆成功')

def register():
user_name = input('请输入账号:')
keyword = input('请输入密码:')
all_users = get_user_name()
if user_name in all_users:
print('注册失败!用户已注册')
return
all_users[user_name] = keyword
with open('user.json', 'w',encoding='utf-8') as f:
json.dump(all_users,f)
print('注册成功!')
def main_page():
# with open('main_page.txt','r',encoding='utf-8') as f:
# page = f.read()
# print(page)
while 1:
with open('main_page.txt', 'r', encoding='utf-8') as f:
page = f.read()
print(page)
value = input('请输入1-3:')
if value == '1':
login()
if value == '2':
register()
if value == '3':
return
main_page()

你可能感兴趣的:(03-27homework)