1、實(shí)現(xiàn)目標(biāo)
編寫一個命令行通訊錄程序,可以添加、查詢、刪除通訊錄好友及電話
2、實(shí)現(xiàn)方法
創(chuàng)建一個類來表示一個人的信息。使用字典存儲每個人的對象,名字作為鍵。
使用pickle模塊永久地把這些對象存儲下來。
使用字典內(nèi)建的方法添加、刪除修改人員信息。
3、思維導(dǎo)圖

4、編寫偽代碼
# 1.創(chuàng)建字典用來存儲通訊錄信息# 2.創(chuàng)建人員類,包含姓名、關(guān)系、電話三個屬性# 3.創(chuàng)建操作類,包含增加、查詢、刪除人員,退出,保存并退出五個方法# 4.程序運(yùn)行# 5.判斷通訊錄文件是否存在# 6.如果存在,將文件讀取到personDictionary字典中# 7.如果不存在,提示并創(chuàng)建# 8.while循環(huán)等待讀取指令 # 9.如果指令為addperson,添加通訊錄人員 # 10.如果指令為delperson,刪除通訊錄人員 # 11.如果指令為search,查找通訊錄人員 # 12.如果指令為quit,不保存退出程序 # 13.如果指令為sq,保存更改并退出程序
5、根據(jù)偽代碼編寫代碼
import pickle as pimport os# 1.創(chuàng)建字典用來存儲通訊錄信息personDictionary= {'name':{'relationship':'','tel':''}}relationshipList=['家人','朋友','同事']# 2.創(chuàng)建人員類,包含姓名、關(guān)系、電話三個屬性class Person: def __init__(self,name,relationship= relationshipList[1],tel='None'): personDictionary[name]= {'relationship':relationship,'tel':tel}# 3.創(chuàng)建操作類,包含增加、查詢、刪除人員,退出,保存并退出五個方法class Operation: def Addperson(): addname= input('請輸入姓名:') addrelationship= int(input('請選擇分組(0:家人,1:朋友,2:同事):')) addtel= input('請輸入電話:') Person(addname,relationshipList[addrelationship],addtel) def Delperson(): name= input('請輸入要刪除的聯(lián)系人姓名:') del personDictionary[name] def Search(): name= input('請輸入要查找的聯(lián)系人的姓名:') if name in personDictionary: print('姓名:%s,關(guān)系:%s,電話:%s' %(name,personDictionary[name]['relationship'],personDictionary[name]['tel'])) else: print('聯(lián)系人不存在。') def Quit(): running= False def SaveQuit(): f= open(addressbookFile,'wb') p.dump(personDictionary,f) f.close() running= False# 4.程序運(yùn)行running= True# 5.判斷通訊錄文件是否存在addressbookFile= 'addressbook.data'# 6.如果存在,將文件讀取到personDictionary字典中if os.path.exists(addressbookFile): f= open(addressbookFile,'rb') personDictionary= p.load(f)# 7.如果不存在,提示并創(chuàng)建else: jCommand= input('未找到通訊錄文件,是否創(chuàng)建?yes/no ') if jCommand== 'yes': f= open(addressbookFile,'wb') p.dump(personDictionary,f) f.close() elif jCommand== 'no': running= False# 8.while循環(huán)等待讀取指令while running: command= input('請輸入指令:') # 9.如果指令為addperson,添加通訊錄人員 if command== 'addperson': Operation.Addperson() continue # 10.如果指令為delperson,刪除通訊錄人員 elif command== 'delperson': Operation.Delperson() continue # 11.如果指令為search,查找通訊錄人員 elif command== 'search': Operation.Search() continue # 12.如果指令為quit,不保存退出程序 elif command== 'quit': Operation.Quit() break # 13.如果指令為sq,保存更改并退出程序 elif command== 'sq': Operation.SaveQuit() break else: print('未找到指令!') continue6、演示
Python3 addressbook.py 請輸入指令:search請輸入要查找的聯(lián)系人的姓名:zhangsan聯(lián)系人不存在。請輸入指令:addperson請輸入姓名:zhangsan請選擇分組(0:家人,1:朋友,2:同事):1請輸入電話:1234567請輸入指令:search請輸入要查找的聯(lián)系人的姓名:zhangsan姓名:zhangsan,關(guān)系:朋友,電話:1234567請輸入指令:sq$ Python3 addressbook.py 請輸入指令:search請輸入要查找的聯(lián)系人的姓名:zhangsan姓名:zhangsan,關(guān)系:朋友,電話:1234567請輸入指令:addperson請輸入姓名:lisi請選擇分組(0:家人,1:朋友,2:同事):1請輸入電話:1234567請輸入指令:q未找到指令!請輸入指令:quit$ Python3 addressbook.py 請輸入指令:search請輸入要查找的聯(lián)系人的姓名:lisi聯(lián)系人不存在。請輸入指令:search請輸入要查找的聯(lián)系人的姓名:zhangsan姓名:zhangsan,關(guān)系:朋友,電話:1234567請輸入指令:quit
總結(jié)
以上就是Python實(shí)現(xiàn)命令行通訊錄實(shí)例教程的全部內(nèi)容,如果有疑問可以留言討論,希望本文的內(nèi)容對大家學(xué)習(xí)使用python能有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選