您好,登錄后才能下訂單哦!
首先新建項目,包含主程序cards_main和工具庫cards_tools:
如下:
main函數主主要使用while和if實現4功能切換,切換的具體操作由cards_tools中的函數執行。不說了直接上代碼,代碼都比較簡單:
(1)cards_main:
"""
File_name : cards_main.py
Author : jack
Date : 2018.7.18
Function : 名片管理系統主程序
"""
import cards_tools
while True:
?? cards_tools.cards_dis()
?? cards_input = input("請選擇操作功能:")
?? if cards_input in ["1", "2", "3"]:
????? if cards_input == "1":
???????? print("- " 20)
???????? print("您選擇輸入的操作代碼是: 1" )
???????? cards_tools.cards_add()
????? elif cards_input == "2":
???????? print("- " 20)
???????? print("您選擇輸入的操作代碼是: 2" )
???????? cards_tools.cards_show_all()
????? elif cards_input == "3":
???????? print("- " 20)
???????? print("您選擇輸入的操作代碼是: 3" )
???????? cards_tools.cards_requ()
?? elif cards_input == "0":
????? print("您選擇輸入的操作代碼是: 0" )
????? print("即將退出系統......\n" "已退出系統,歡迎您下次【名片管理系統】!")
????? break
?? else:
????? cards_input = input("輸入錯誤,請重新輸入:")
????? cards_tools.cards_dis()
????? #cards_input = input("輸入錯誤,請重新輸入:")
????? while True:
???????? cards_input = input("輸入錯誤,請重新輸入:")
???????? cards_tools.cards_dis()
???????? if cards_input in ["1", "2", "3", "0"]:
??????????? if cards_input == "1":
?????????????? print("- " 20)
?????????????? print("您選擇輸入的操作代碼是: 1")
?????????????? cards_tools.cards_add()
?????????????? break
??????????? if cards_input == "2":
?????????????? print("- " 20)
?????????????? print("您選擇輸入的操作代碼是: 2")
?????????????? cards_tools.cards_show_all()
?????????????? break
??????????? if cards_input == "3":
?????????????? print("- " 20)
?????????????? print("您選擇輸入的操作代碼是: 3")
?????????????? cards_tools.cards_requ()
?????????????? break
??????????? else:
??????????? # #print("返回主菜單")
?????????????? break
????? break
(2)cards_tools:
"""
File_name : cards_main.py
Author : jack
Date : 2018.7.18
Function : 名片管理系統tools
"""
def cards_dis():
?? """顯示菜單"""
?? print(" " 20)
?? print("\t歡迎使用【名片管理系統 V1.0】\n"
????? "\t\t\t1. 新建名片\n" "\t\t\t2. 顯示全部\n" "\t\t\t3. 查詢名片\n\n" "\t\t\t0. 退出系統")
?? print(" " 20)
cards_list = []
def cards_add ():
?? """新增名片"""
?? print("- " * 20 )
?? print("新增名片")
?? """輸入-整理成字典-將字典存儲到列表中-提示輸入成功"""
?? name_str = input("請輸入姓名:")
?? gender_str = input("請輸入性別:")
?? tel_int = input("請輸入電話:")
?? add_str = input("請輸入地址:")
?? cards_dict = {"name" : name_str,
????????????? "gender" : gender_str,
????????????? "telephone" : tel_int,
????????????? "address" : add_str}
?? cards_list.append(cards_dict)
?? print(cards_list)
?? print("%s 的名片新增成功!" % name_str)
def cards_show_all ():
?? """顯示全部名片"""
?? print("- " 20)
?? print("顯示所有名片")
?? if len(cards_list) == 0:
????? print("當前系統中沒有名片,請新增名片!")
?? else:
????? print("=" 40)
????? #print("姓名\t\t" "性別\t\t" "電話\t\t" "地址")
????? for name in ("姓名","性別","電話","地址"):
???????? print(name, end="\t\t")
????? print(" ")
????? for cards_dict in cards_list:
???????? print("%s\t\t%s\t\t%s\t\t%s" % (cards_dict["name"],
???????????????????????????????? cards_dict["gender"],
???????????????????????????????? cards_dict["telephone"],
???????????????????????????????? cards_dict["address"]))
???????? print(cards_dict)
????? print("=" * 40)
def cards_requ ():
?? """查詢名片"""
?? print("- " 20)
?? print("查詢名片")
?? find_name = input("請輸入要搜索的姓名:")
?? if len(cards_list) == 0:
????? print("抱歉!沒找到用戶 %s 的信息, 請新增!" % find_name)
?? else:
????? for search_name in cards_list:
???????? if search_name["name"] == find_name:
??????????? print("找到 %s 的信息了。" % find_name)
??????????? print("=" 40)
??????????? for name in ("姓名", "性別", "電話", "地址"):
?????????????? print(name, end="\t\t")
??????????? print(" ")
??????????? print("s%\t\ts%\t\ts%\t\ts%" % (search_name["name"],
??????????????????????????????????? search_name["gender"],
??????????????????????????????????? search_name["telephone"],
??????????????????????????????????? search_name["address"]))
??????????? print("=" * 40)
??????????? cards_deal(search_name)
??????????? break
???????? else:
??????????? print("抱歉!沒找到用戶 %s 的信息。" % find_name)
def cards_deal(find_dict):
?? """修改和刪除名片"""
?? deal_cards = input("請選擇要進行的操作: [1] 刪除\t\t[2] 修改\t\t[3] 返回主菜單\n")
?? if deal_cards in ["1","2","3"]:
????? if deal_cards == "1":
???????? cards_list.remove(find_dict)
???????? print("刪除用戶 %s 名片成功!" % (find_dict["name"]))
????? if deal_cards == "2":
???????? find_dict["name"] = cards_mod(find_dict["name"], "請輸入修改后的姓名:")
???????? find_dict["gender"] = cards_mod(find_dict["gender"], "請輸入修改后的性別:")
???????? find_dict["telephone"] = cards_mod(find_dict["telephone"], "請輸入修改后的電話:")
???????? find_dict["address"] = cards_mod(find_dict["address"], "請輸入修改后的地址:")
???????? print("用戶 %s 的名片修改成功!" % find_dict["name"])
?? else:
???????? print("輸入錯誤,請重新輸入!")
def cards_mod(cards_value, cards_new):
?? result = input(cards_new)
?? if len(result) > 0:
????? return result
?? else:
????? return cards_value
本人也是新手,歡迎大佬們批評指正。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。