【Python】生死簿管理系统,估值5毛

生死簿管理系统

代码

"""
生死簿管理系统
"""
import os
import time

file_name = 'data.txt'


def main():
    while True:
        main_menu()
        choice = (int)(input("请选择: "))
        if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
            if choice == 0:
                answer = input("确定要退出生死簿系统吗?y/n ")
                if answer.lower() == 'y':
                    print("已退出!!!")
                    break
                else:
                    continue
            elif choice == 1:
                add()
                pass
            elif choice == 2:
                pass
            elif choice == 3:
                delete()
                pass
            elif choice == 4:
                pass
            elif choice == 5:
                pass
            elif choice == 6:
                pass
            elif choice == 7:
                pass
        else:
            print("")


def main_menu():
    print("=============================生死簿管理系统=============================")
    print("-----------------------------功能菜单-----------------------------")
    print("1.录入人员")
    print("2.查找人员")
    print("3.删除人员")
    print("4.修改人员")
    print("5.排序")
    print("6.统计总人数")
    print("7.查询所有")
    print("0.退出")
    print("----------------------------------------------------------------")


def add():
    person = {}
    while True:
        person_id = input("请输入人员ID: ")
        if not id:
            print("id不能为空")
            continue
        else:
            person["id"] = person_id
            break
    while True:
        name = input("请输入人员姓名: ")
        if not name:
            print("id不能为空")
            continue
        else:
            person["name"] = name
            break
    while True:
        num = input("请输入人员阳寿: ")
        if not num:
            print("阳寿不能为空")
            continue
        else:
            person["num"] = num
            break

    # 保存数据
    save_data(person)
    # 是否继续添加
    s = input("是否继续添加人员?y/n ")
    if s.lower() == 'y':
        add()
    else:
        main()
    pass


def save_data(person):
    print("数据保存中。。。")
    try:
        # 追加
        file_stream = open(file_name, 'a', encoding="utf-8")
    except Exception as e:
        # 写入
        file_stream = open(file_name, 'w', encoding="utf-8")
    print("数据保存成功。。。")
    # 写入 数据状态
    person['yn'] = 1
    # 创建时间
    person['create_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    file_stream.write(str(person) + "\n")
    # 关闭文件
    file_stream.close()
    pass


def search():
    pass


def delete():
    while True:
        person_id = input("请输入要删除的人员ID:")
        person_list = []
        if person_id != '' and person_id is not None:
            if os.path.exists(file_name):
                with open(file_name, 'r', encoding="utf-8") as file:
                    person_list = file.readlines()
            else:
                pass
            # 定义是否被删除
            del_flag = False
            if person_list:
                # w模式会清空内容
                with open(file_name, 'w', encoding="utf-8") as wfile:
                    for item in person_list:
                        # 转为dict类型
                        d = dict(eval(item))
                        if d['id'] != person_id:
                            wfile.write(str(d) + "\n")
                        else:
                            del_flag = True
                    if del_flag:
                        print(f"ID为{person_id}的人员已删除!!!")
                    else:
                        print(f'ID为{person_id}的人员不存在!!!')
            else:
                print(f'ID为{person_id}的人员不存在!!!')
                break
            query_all()
            while True:
                answer = input("是否继续删除?y/n ")
                if answer == '' or answer is None:
                    continue
                else:
                    break
            if answer.lower() == 'y':
                continue
            else:
                break
        else:
            print("ID不能为空")
    pass


def modify():
    pass


def sort():
    pass


def total():
    pass


def query_all():
    pass


if __name__ == '__main__':
    main()

打包可执行文件

pip3 install PyInstaller

pyinstaller -F /Users/kw/kwzone/Python/code/python-hello/实操篇/BookOfLifeAndDeath/bookmgr.py

【Python】生死簿管理系统,估值5毛_第1张图片
【Python】生死簿管理系统,估值5毛_第2张图片
【Python】生死簿管理系统,估值5毛_第3张图片

你可能感兴趣的:(Python,python)