| from datetime import datetime |
| |
| |
| |
| |
| |
| |
| |
| |
| time = f'时间为 :>>>> {datetime.now().strftime("%Y年%m月%d日 %H时%M分%S秒")}' |
| |
| login_data = {'username': '', 'pwd': '', 'is_admin': False} |
| |
| |
| def check_login(tag): |
| if login_data.get('username') != '': |
| if tag == 'admin': |
| if login_data.get('username') == 'lea4ning': |
| login_data['is_admin'] = True |
| return True, None |
| else: |
| return False, f"当前功能只有管理员可以使用!" |
| elif tag == 'user': |
| pass |
| else: |
| return False, f"请先登录!" |
| |
| |
| def user_input(): |
| username = input("请输入用户名:").strip() |
| user_pwd = input("请输入密码:").strip() |
| return username, user_pwd |
| |
| |
| def register(): |
| |
| |
| username, user_pwd = user_input() |
| data = read_data('user_login.txt', 'login') |
| login_dict = {} |
| if username == 'lea4ning' and user_pwd == '12345': |
| user_limit = 'admin' |
| elif username in data.keys(): |
| return False, "用户已注册,请登录" |
| else: |
| user_limit = 'user' |
| login_dict[username] = {'pwd': user_pwd, 'limit': user_limit} |
| save_data(tag="register", data=login_dict) |
| str_data = f"{username}:>>>>>注册成功>>>{time}" |
| print(str_data) |
| flag, msg = save_file(path='user_log.txt', str_data=str_data) |
| return flag, msg |
| |
| |
| def add_user(): |
| flag, msg = check_login('admin') |
| if flag: |
| flag, msg = add_info('add_user') |
| return flag, msg |
| |
| |
| def add_info(tag): |
| data = read_data('user_info.txt', 'info') |
| username = '' |
| user_pwd = '' |
| user_sex = '' |
| user_id = '' |
| user_age = '' |
| hobby_list = [] |
| if tag == 'renew': |
| renew_name = list_id('renew') |
| username = renew_name |
| else: |
| username, user_pwd = user_input() |
| while True: |
| print("信息提交".center(30, '-')) |
| if tag == 'renew': |
| user_id = data[username]['id'] |
| else: |
| user_id = input("请输入编号(编号不可重复):").strip() |
| if user_id in [key['id'] for key in data.values()]: |
| print("此编号已存在,请重试!") |
| continue |
| |
| user_age = input("请输入年龄(请输入纯数字):").strip() |
| if not user_age.isdigit(): |
| print("请输入纯数字!") |
| continue |
| |
| user_sex = input("请输入性别(请在男或女中选择:").strip() |
| |
| if user_sex not in ['男', '女']: |
| print(f"输入有误,错误内容{user_sex},请重新输入") |
| continue |
| while True: |
| hobby_input = input("请输入爱好(q结束添加):").strip() |
| if tag == 'renew': |
| hobby_list = data[username]['hobby'] |
| if hobby_input.upper() == 'Q': |
| break |
| elif hobby_input in hobby_list: |
| print(f"请不要重复添加,{hobby_input}已经存在!") |
| continue |
| else: |
| hobby_list.append(hobby_input) |
| break |
| info_dict = {username: {'id': user_id, 'name': username, 'age': user_age, 'sex': user_sex, 'hobby': hobby_list}} |
| flag, msg = save_data(tag=tag, data=info_dict) |
| return flag, msg |
| |
| |
| def save_data(tag, data): |
| str_data = '' |
| path = '' |
| if tag == 'register': |
| path = 'user_login.txt' |
| for key, value in data.items(): |
| str_data = f"{key}|{value.get('pwd')}|{value.get('limit')}" |
| elif tag == 'add_user': |
| path = 'user_info.txt' |
| for key, value in data.items(): |
| str_hobby = '-'.join(value.get('hobby')) |
| str_data = f"{value.get('id')}|{key}|{value.get('age')}|{value.get('sex')}|{str_hobby}" |
| elif tag == 'del_one': |
| path = 'user_info.txt' |
| del_name = data |
| str_data = f"None|{del_name}|None|None|None" |
| elif tag == 'renew': |
| path = 'user_info.txt' |
| for key, value in data.items(): |
| str_hobby = '-'.join(value.get('hobby')) |
| str_data = f"{value.get('id')}|{key}|{value.get('age')}|{value.get('sex')}|{str_hobby}" |
| flag, msg = save_file(path=path, str_data=str_data) |
| return flag, msg |
| |
| |
| def save_file(path, str_data): |
| print(str_data) |
| try: |
| with open(path, 'a', encoding='utf-8') as fp: |
| fp.write(str_data + '\n') |
| return True, f"文件更改成功,可在{path}中查看!" |
| except Exception as e: |
| return False, f"文件写入失败{e}!" |
| |
| |
| def read_data(path, tag): |
| try: |
| data = {} |
| with open(path, 'r', encoding='utf8') as fp: |
| list_data = fp.read().split('\n') |
| if tag == 'login': |
| for login in list_data: |
| |
| if len(login) == 0: |
| continue |
| |
| username, user_pwd, limit = login.split('|') |
| data[username] = {'pwd': user_pwd, 'limit': limit} |
| elif tag == 'info': |
| for info in list_data: |
| if info == '': |
| continue |
| elif 'None' in info: |
| continue |
| user_id, username, user_age, user_sex, hobby_list = info.split('|') |
| hobby = hobby_list.split('-') |
| data[username] = {'id': user_id, 'age': user_age, 'sex': user_sex, 'hobby': hobby} |
| return data |
| except Exception as e: |
| return f"文件读取失败!错误信息为:{e}" |
| |
| |
| def login(): |
| |
| username, user_pwd = user_input() |
| data = read_data(path='user_login.txt', tag='login') |
| if username not in data.keys(): |
| return False, f"{username}尚未注册,请先注册!" |
| true_pwd = data[username].get('pwd') |
| if user_pwd == true_pwd: |
| str_data = f"{username}:>>>>>登录成功>>>{time}" |
| login_data['username'] = username |
| login_data['pwd'] = user_pwd |
| save_file(path='user_log.txt', str_data=str_data) |
| return True, f"登录成功!欢迎{username}!" |
| else: |
| return False, "登录失败!" |
| |
| |
| def list_id(tag): |
| data = read_data(path='user_info.txt', tag='info') |
| list_id = [data[key].get('id') for key in data.keys()] |
| |
| dict_id = {key: keys for key in list_id for keys, value in data.items() if value['id'] == key} |
| print(f"当前所有id如下:\n{dict_id}") |
| choice = '' |
| if tag == 'check': |
| choice = input("请输入需要查看的用户id:") |
| elif tag == 'del': |
| choice = input("请输入需要删除的用户id:") |
| elif tag == 'renew': |
| choice = input("请输入需要修改的用户id:") |
| for check_id in dict_id: |
| if check_id == choice: |
| check_name = dict_id[choice] |
| return check_name |
| else: |
| return "输入编号有误,请重新输入!" |
| |
| |
| def check_one(): |
| |
| data = read_data(path='user_info.txt', tag='info') |
| check_name = list_id('check') |
| txt = data[check_name] |
| return True, txt |
| |
| |
| def check_all(): |
| |
| data = read_data(path='user_info.txt', tag='info') |
| return True, data |
| |
| |
| def del_one(): |
| |
| check_login('admin') |
| del_name = list_id('del') |
| print(f"已将信息删除") |
| flag, msg = save_data(tag='del_one', data=del_name) |
| return flag, msg |
| |
| |
| def del_all(): |
| check_login('admin') |
| |
| try: |
| with open('user_info.txt', 'w', encoding='utf8') as fp: |
| fp.write('') |
| return True, "信息库已清空!" |
| except Exception as e: |
| return False, f"删除失败!错误信息:{e}" |
| |
| |
| def renew_info(): |
| flag, msg = check_login('admin') |
| |
| if flag: |
| flag, msg = add_info('renew') |
| return flag, msg |
| |
| |
| def back_system(): |
| |
| return False, 'break' |
| |
| |
| |
| func_menu = ''' |
| --------------- 欢迎来到员工管理系统 --------------- |
| 1:注册 |
| 2:登陆 |
| 3:添加员工信息 |
| 4:查看指定员工信息 |
| 5:查看所有员工信息 |
| 6:删除指定员工信息 |
| 7:删除所有员工信息 |
| 8:修改指定员工信息 |
| 9:退出系统 |
| ''' |
| func_dict = { |
| 1: register, |
| 2: login, |
| 3: add_user, |
| 4: check_one, |
| 5: check_all, |
| 6: del_one, |
| 7: del_all, |
| 8: renew_info, |
| 9: back_system |
| } |
| |
| |
| def main_system(): |
| while True: |
| print(func_menu) |
| func_id = input("请输入功能ID :>>>> ").strip() |
| if not func_id.isdigit(): |
| print(f'{func_id} :>>>> 非法字符') |
| continue |
| func_id = int(func_id) |
| if func_id not in func_dict.keys(): |
| print(f"{func_id} :>>>> 不存在该功能!") |
| continue |
| |
| |
| |
| |
| |
| func = func_dict.get(func_id) |
| flag, msg = func() |
| if flag: |
| print(msg) |
| elif not flag and msg == 'break': |
| print(f"欢迎下次使用!再见!") |
| break |
| else: |
| print(msg) |
| continue |
| |
| |
| if __name__ == '__main__': |
| main_system() |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了