生死簿管理系統
代碼
"""
生死簿管理系統
"""
import os
import timefile_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("已退出!!!")breakelse:continueelif choice == 1:add()passelif choice == 2:passelif choice == 3:delete()passelif choice == 4:passelif choice == 5:passelif choice == 6:passelif choice == 7:passelse: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不能為空")continueelse:person["id"] = person_idbreakwhile True:name = input("請輸入人員姓名: ")if not name:print("id不能為空")continueelse:person["name"] = namebreakwhile True:num = input("請輸入人員陽壽: ")if not num:print("陽壽不能為空")continueelse:person["num"] = numbreak# 保存數據save_data(person)# 是否繼續添加s = input("是否繼續添加人員?y/n ")if s.lower() == 'y':add()else:main()passdef 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()passdef search():passdef 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 = Falseif 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 = Trueif del_flag:print(f"ID為{person_id}的人員已刪除!!!")else:print(f'ID為{person_id}的人員不存在!!!')else:print(f'ID為{person_id}的人員不存在!!!')breakquery_all()while True:answer = input("是否繼續刪除?y/n ")if answer == '' or answer is None:continueelse:breakif answer.lower() == 'y':continueelse:breakelse:print("ID不能為空")passdef modify():passdef sort():passdef total():passdef query_all():passif __name__ == '__main__':main()
打包可執行文件
pip3 install PyInstallerpyinstaller -F /Users/kw/kwzone/Python/code/python-hello/實操篇/BookOfLifeAndDeath/bookmgr.py