import hashlib import json import os # 账户新是一个保存在文件中字典 def load_account(path): if os.path.exists(path): return json.load(open(path, "rt", encoding="utf-8")) else: return {} # 将用户信息字典保存到硬盘中 def save_account(account_info, path): json.dump(account_info, open(path, "wt", encoding="utf-8")) UI = """ 1.注册 2.登陆 0.退出 """ while True: print(UI) select = int(input("请输入你的选择:")) if select == 1: # 注册 # 用输入账户密码,确认密码 account = input("请输入账户:") dc = load_account("account.txt") if dc.get(account): print("该账户已存在,请重新注册") else: passwort = input("请输入密码:") confirm = input("请再次输入密码:") if passwort == confirm: # 密码加密 hs_obj = hashlib.md5(passwort.encode()) dc[account] = hs_obj.hexdigest() # 将账户密码保存到文件 save_account(dc, "account.txt") else: print("前后两次密码输入不一致") elif select == 2: acc = input("请输入账号") dc = load_account("account.txt") if dc.get(acc): passwort = input("请输入密码") hs_obj = hashlib.md5(passwort.encode()) if any(dc(acc) == hs_obj.hexdigest() for value in dc.values()): print("登陆成功") else:print("密码错误") else:print("账号错误") elif select == 0: sys.exit()
用python做登陆系统
最新推荐文章于 2024-08-27 09:41:07 发布