Python編寫電話薄實(shí)現(xiàn)增刪改查功能
初學(xué)python,寫一個(gè)小程序練習(xí)一下。主要功能就是增刪改查的一些功能。主要用到的技術(shù):字典的使用,pickle的使用,io文件操作。代碼如下:
import pickle #studentinfo = {'netboy': '15011038018',\ # 'godboy': '15011235698'} studentinfo = {} FUNC_NUM = 5 def write_file(value): file = open('student_info.txt', 'wb') file.truncate() pickle.dump(value, file, True) file.close def read_file(): global studentinfo file = open('student_info.txt', 'rb') studentinfo = pickle.load(file) file.close() def search_student(): global studentinfo name = input('please input student\'s name:') if name in studentinfo: print('name:%s phone:%s' % (name, studentinfo[name])) else: print('has no this body') def delete_student(): global studentinfo name = input('please input student\'s name:') if name in studentinfo: studentinfo.pop(name) write_file(studentinfo) else: print('has no this body') def add_student(): global studentinfo name = input('please input student\'s name:') phone = input('please input phone:') studentinfo[name] = phone write_file(studentinfo) def modifiy_student(): global studentinfo name = input('please input student\'s name:') if name in studentinfo: phone = input('please input student\'s phone:') studentinfo[name] = phone else: print('has no this name') def show_all(): global studentinfo for key, value in studentinfo.items(): print('name:' + key + 'phone:' + value) func = {1 : search_student, \ 2 : delete_student, \ 3 : add_student, \ 4 : modifiy_student, \ 5 : show_all} def menu(): print('-----------------------------------------------'); print('1 search student:') print('2 delete student:') print('3 add student:') print('4 modifiy student:') print('5 show all student') print('6 exit') print('-----------------------------------------------'); def init_data(): global studentinfo file = open('student_info.txt', 'rb') studentinfo = pickle.load(file) #print(studentinfo) file.close() init_data() while True: menu() index = int(input()) if index == FUNC_NUM + 1: exit() elif index < 1 or index > FUNC_NUM + 1: print('num is between 1-%d' % (FUNC_NUM + 1)) continue #print(index) func[index]()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
利用Python批量導(dǎo)出mysql數(shù)據(jù)庫(kù)表結(jié)構(gòu)的操作實(shí)例
這篇文章主要給大家介紹了關(guān)于利用Python批量導(dǎo)出mysql數(shù)據(jù)庫(kù)表結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2022-08-08python arcpy練習(xí)之面要素重疊拓?fù)錂z查
今天小編就為大家分享一篇Python ArcPy的面要素重疊拓?fù)錂z查,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-09-09如何用python實(shí)現(xiàn)一個(gè)HTTP連接池
這篇文章主要介紹了如何用python實(shí)現(xiàn)一個(gè)HTTP連接池的步驟,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01Python開發(fā)之基于模板匹配的信用卡數(shù)字識(shí)別功能
這篇文章主要介紹了基于模板匹配的信用卡數(shù)字識(shí)別功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Python人工智能之混合高斯模型運(yùn)動(dòng)目標(biāo)檢測(cè)詳解分析
運(yùn)動(dòng)目標(biāo)檢測(cè)是計(jì)算機(jī)視覺領(lǐng)域中的一個(gè)重要內(nèi)容,其檢測(cè)效果將會(huì)對(duì)目標(biāo)跟蹤與識(shí)別造成一定的影響,本文將介紹用Python來(lái)進(jìn)行混合高斯模型運(yùn)動(dòng)目標(biāo)檢測(cè),感興趣的朋友快來(lái)看看吧2021-11-11python opencv圖片編碼為h264文件的實(shí)例
今天小編就為大家分享一篇python opencv圖片編碼為h264文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12在Django的URLconf中進(jìn)行函數(shù)導(dǎo)入的方法
這篇文章主要介紹了在Django的URLconf中進(jìn)行函數(shù)導(dǎo)入的方法,Django是Python的最為著名的開發(fā)框架,需要的朋友可以參考下2015-07-07使用python-cv2實(shí)現(xiàn)Harr+Adaboost人臉識(shí)別的示例
這篇文章主要介紹了使用python-cv2實(shí)現(xiàn)Harr+Adaboost人臉識(shí)別的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10