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]()
以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python標(biāo)準(zhǔn)庫shutil模塊使用方法解析
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫shutil模塊使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
超好玩的"隔空操物"通過Python?MediaPipe庫實(shí)現(xiàn)
這篇文章主要介紹了python+mediapipe+opencv實(shí)現(xiàn)手部關(guān)鍵點(diǎn)檢測(cè)功能(手勢(shì)識(shí)別),本文僅僅簡單介紹了mediapipe的使用,而mediapipe提供了大量關(guān)于圖像識(shí)別等的方法,需要的朋友可以參考下2022-01-01
詳解如何在Python中有效調(diào)用JavaScript
JavaScript和Python都是極為流行的編程語言,并在前端開發(fā)和后端開發(fā)領(lǐng)域扮演著重要的角色,那么Python如何更好的契合JavaScript呢,下面就跟隨小編一起學(xué)習(xí)一下吧2024-02-02
對(duì)python的unittest架構(gòu)公共參數(shù)token提取方法詳解
今天小編就為大家分享一篇對(duì)python的unittest架構(gòu)公共參數(shù)token提取方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
對(duì)python requests的content和text方法的區(qū)別詳解
今天小編就為大家分享一篇對(duì)python requests的content和text方法的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
tensorflow實(shí)現(xiàn)二維平面模擬三維數(shù)據(jù)教程
今天小編就為大家分享一篇tensorflow實(shí)現(xiàn)二維平面模擬三維數(shù)據(jù)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02

