分文件編寫:
1. ContactMain.py
2. ContactCards.py
函數列表:
1. MainPage()
2. CreatContact()
3. FindContact()
4. CheckAllContact()
5. UpdateContact()
編譯環境:Ubuntu18.04 Python3 & Pycharm
Shell執行請自行賦予可執行權限(亦可直接解釋執行),命令如下:
chmod -x FileName.py
File1 Code:
#! /usr/bin/python3.6
# 導入增刪改查工具包
import ContactCards
# 無限循環進入功能選擇
while True:
# 顯示主頁
ContactCards.MainPage()
# 接收選項
OptionKey = input("Input Option:")
if OptionKey in ["1", "2", "3"]:
if OptionKey == "1":
ContactCards.CreatContact()
elif OptionKey == "2":
ContactCards.FindContact()
elif OptionKey == "3":
ContactCards.CheckAllContact()
# 退出
elif OptionKey == "0":
print("Good Bye~~~")
break
# 輸入錯誤
else:
print("Input option error\n")
File2 Code:
ContactList = []
def MainPage():
"""首頁菜單"""
print("*" * 50)
print("\t\t\t\tContact Management")
print(" ")
print("\t\t\t1. Creat new contact")
print("\t\t\t2. Find a contact")
print("\t\t\t3. Check all contact")
print("\t\t\t0. EXIT")
print("*" * 50)
def CreatContact():
"""創建新聯系人"""
# 接收各種信息
NameTemp = input("Name: ")
PhoneTemp = input("Phone: ")
EmailTemp = input("Email: ")
WechatTemp = input("Wechat: ")
# 插入到聯系人列表
ContactDict = {
"Name": NameTemp, "Phone": PhoneTemp, "Email": EmailTemp, "Wechat": WechatTemp}
ContactList.append(ContactDict)
print("-" * 50)
print("OK! %s Information Add Success!" % ContactDict["Name"])
def FindContact():
""""查找聯系人"""
# 接收查找聯系人姓名
NameTemp = input("Input Find Name: ")
print("-" * 50)
# 遍歷查找
for Temp in ContactList:
# 找到
if NameTemp == Temp["Name"]:
print("Find Success")
print("=" * 50)
print("Name\t\tPhone\t\tEmail\t\tWechat")
print("%s\t\t%s\t\t%s\t\t%s" % (Temp["Name"], Temp["Phone"], Temp["Email"], Temp["Wechat"]))
# 修改信息選項
print("Select: [1]Update [2]Delete [0]Exit")
OptionKey = input("Input Option: ")
# 修改,回車不修改
if OptionKey == "1":
Temp["Name"] = UpadteContact(Temp["Name"], input("Name(Input Enter No MOdify): "))
Temp["Phone"] = UpadteContact(Temp["Phone"], input("Phone(Input Enter No Modify): "))
Temp["Email"] = UpadteContact(Temp["Email"], input("Email(Input Enter No Modify): "))
Temp["Wechat"] = UpadteContact(Temp["Wechat"], input("Wechat(Input Enter No Modify): "))
print("Update success")
return
# 刪除
elif OptionKey == '2':
ContactList.remove(Temp)
print("Delect Success!")
return
# 輸入0或者其他
else:
return
# 遍歷未找到
else:
print("Not Found!")
def CheckAllContact():
"""查看所有聯系人"""
# 聯系人為空情況下
if len(ContactList) == 0:
print("Contact List NULL!")
return
# 有聯系人則格式化輸出
print("=" * 50)
print("Name\t\tPhone\t\tEmail\t\tWechat")
for Temp in ContactList:
print("%s\t\t%s\t\t%s\t\t%s" % (Temp["Name"], Temp["Phone"], Temp["Email"], Temp["Wechat"]))
print("Have %d Constct!" % len(ContactList))
def UpadteContact(SourceInfo, UpdateInfo):
"""作為回車不修改輔助函數"""
# 有新信息輸入
if len(UpdateInfo) > 0:
return UpdateInfo
# 檢測沒有輸入(回車)
else:
return SourceInfo
運行截圖: