用python寫的新手通訊錄小程式
內容概要:
1,聯系人的新建及洗掉查找等用字典的方法,
2,存盤采用pickle模塊將字典保存到檔案中,
3,代碼可直接運行,第一次運行會初始化生成一個檔案,第二次打開即可正常運行
import pickle
print('歡迎進入通訊錄程式')
print('1:查詢聯系人資料')
print('2:插入新的聯系人')
print('3:洗掉已有聯系人')
print('4:顯示所有聯系人')
print('5:退出通訊錄程式')
try:
txl=open('永久通訊錄3.pkl','rb')
except FileNotFoundError:
txl=open('永久通訊錄3.pkl','wb')
print('第一次初始化完成,請重新打開')
try:
dict1=pickle.load(txl)
except EOFError:
dict1={}
txl.close()
while 1:
try:
a=int(input('請輸入相關的指令代碼:'))
except ValueError:
print('輸入有誤,請重新輸入')
continue
if a==1:
name=str(input('請輸入聯系人姓名:'))
if name not in dict1.keys():
print('你沒有添加該聯系人')
else:
print(dict1[name])
elif a==2:
name=input('請輸入聯系人姓名:')
number=input('請輸入聯系人電話:')
if name in dict1.keys():
YN=input('該用戶已經存在,是否修改電話號碼(Y/N')
if YN=='Y':
number=input('請輸入新的電話號碼:')
dict1[name]=number
else:
continue
else:
dict1[name]=number
elif a==3:
name=str(input('請輸入需要洗掉的聯系人姓名:'))
if name not in dict1.keys():
print('你沒有該聯系人')
else:
del dict1[name]
elif a==4:
print(dict1)
else:
txl=open('永久通訊錄3.pkl','wb')
pickle.dump(dict1,txl)
txl.close()
break
print('謝謝使用')
后言:
1,新手入門,懇請大佬指點,
2,歡迎留言
3,創作不易,愿意打賞者,在這里謝過!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/276199.html
標籤:python
上一篇:pywin32介紹到入門
下一篇:os模塊的簡單使用
