diff --git a/main.py b/main.py index 4c0c135f61696bcf42c375ca5ab62aa5b105afc8..4902b099068c63ec0f813d05e5acb81b261788d8 100644 --- a/main.py +++ b/main.py @@ -1 +1,73 @@ -print('欢迎来到 InsCode') \ No newline at end of file +import json + +# 加载已存储的密码数据 +try: + with open('passwords.json', 'r') as f: + passwords = json.load(f) +except FileNotFoundError: + passwords = {} + +# 菜单功能 +def menu(): + print('请选择操作:') + print('1. 存储密码') + print('2. 获取密码') + print('3. 删除密码') + print('4. 查看所有密码') + print('5. 退出程序') + +# 存储密码 +def save_password(): + name = input('请输入网站或应用的名称:') + username = input('请输入登录用户名:') + password = input('请输入登录密码:') + passwords[name] = {'username': username, 'password': password} + with open('passwords.json', 'w') as f: + json.dump(passwords, f) + print('密码保存成功!') + +# 获取密码 +def get_password(): + name = input('请输入要查看的网站或应用名称:') + if name in passwords: + print('账号:', passwords[name]['username']) + print('密码:', passwords[name]['password']) + else: + print('未找到该网站或应用的密码!') + +# 删除密码 +def delete_password(): + name = input('请输入要删除的网站或应用名称:') + if name in passwords: + del passwords[name] + with open('passwords.json', 'w') as f: + json.dump(passwords, f) + print('密码删除成功!') + else: + print('未找到该网站或应用的密码!') + +# 查看所有密码 +def view_passwords(): + if not passwords: + print('暂无保存密码!') + else: + for name, data in passwords.items(): + print(name, ':', data['username'], '/', data['password']) + +# 主循环 +while True: + menu() + choice = input('请输入您的选择:') + if choice == '1': + save_password() + elif choice == '2': + get_password() + elif choice == '3': + delete_password() + elif choice == '4': + view_passwords() + elif choice == '5': + print('感谢使用密码管理器,再见!') + break + else: + print('输入有误,请重新输入!')