Python編寫電話薄實現(xiàn)增刪改查功能
初學(xué)python,寫一個小程序練習(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]()
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)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-08
python arcpy練習(xí)之面要素重疊拓?fù)錂z查
今天小編就為大家分享一篇Python ArcPy的面要素重疊拓?fù)錂z查,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-09-09
Python開發(fā)之基于模板匹配的信用卡數(shù)字識別功能
這篇文章主要介紹了基于模板匹配的信用卡數(shù)字識別功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
Python人工智能之混合高斯模型運(yùn)動目標(biāo)檢測詳解分析
運(yùn)動目標(biāo)檢測是計算機(jī)視覺領(lǐng)域中的一個重要內(nèi)容,其檢測效果將會對目標(biāo)跟蹤與識別造成一定的影響,本文將介紹用Python來進(jìn)行混合高斯模型運(yùn)動目標(biāo)檢測,感興趣的朋友快來看看吧2021-11-11
在Django的URLconf中進(jìn)行函數(shù)導(dǎo)入的方法
這篇文章主要介紹了在Django的URLconf中進(jìn)行函數(shù)導(dǎo)入的方法,Django是Python的最為著名的開發(fā)框架,需要的朋友可以參考下2015-07-07
使用python-cv2實現(xiàn)Harr+Adaboost人臉識別的示例
這篇文章主要介紹了使用python-cv2實現(xiàn)Harr+Adaboost人臉識別的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

