Python編寫電話薄實現(xiàn)增刪改查功能
更新時間:2016年05月07日 15:48:28 作者:net小伙
這篇文章主要為大家詳細介紹了Python編寫電話薄實現(xiàn)增刪改查功能的相關(guān)資料,感興趣的朋友可以參考一下
初學python,寫一個小程序練習一下。主要功能就是增刪改查的一些功能。主要用到的技術(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]()
以上就是本文的全部內(nèi)容,希望對大家學習Python程序設(shè)計有所幫助。
相關(guān)文章
利用Python批量導(dǎo)出mysql數(shù)據(jù)庫表結(jié)構(gòu)的操作實例
這篇文章主要給大家介紹了關(guān)于利用Python批量導(dǎo)出mysql數(shù)據(jù)庫表結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2022-08-08Python開發(fā)之基于模板匹配的信用卡數(shù)字識別功能
這篇文章主要介紹了基于模板匹配的信用卡數(shù)字識別功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01在Django的URLconf中進行函數(shù)導(dǎo)入的方法
這篇文章主要介紹了在Django的URLconf中進行函數(shù)導(dǎo)入的方法,Django是Python的最為著名的開發(fā)框架,需要的朋友可以參考下2015-07-07使用python-cv2實現(xiàn)Harr+Adaboost人臉識別的示例
這篇文章主要介紹了使用python-cv2實現(xiàn)Harr+Adaboost人臉識別的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10