[1].[文件]
address_book.py ~ 4KB 下载(205)
#!/usr/bin/python
import os
import pickle
#判断通讯录是否存在,不存在则创建空通讯录
if os.path.exists(r'E:\Person.data')==False:
f=open('E:\Person.data','wb')
temp={'total' : 0}
pickle.dump(temp,f)
f.close()
else:
pass
#添加联系人
def add():
f=open('E:\Person.data','rb')
a=pickle.load(f)
f.close()
b=0
name = input('请输入所要添加联系人的姓名:')
for key in a.keys():
b+=1
if key&#61;&#61;name and b <&#61; a[&#39;total&#39;]&#43;1:
print("联系人已存在&#xff0c;添加失败&#xff01;")
break
if b&#61;&#61;a[&#39;total&#39;]&#43;1 and key !&#61; name:
number &#61; input(&#39;请输入号码:&#39;)
information&#61;{name : number}
a[&#39;total&#39;]&#43;&#61;1
a.update(information)
f&#61;open(&#39;E:\Person.data&#39;,&#39;wb&#39;)
pickle.dump(a,f)
f.close()
print(&#39;添加成功!&#39;)
break
#显示所有联系人
def showall():
f&#61;open(&#39;E:\Person.data&#39;,&#39;rb&#39;)
a&#61;pickle.load(f)
print("一共有{}个联系人.".format(a[&#39;total&#39;]))
for key in a.keys():
if key !&#61; &#39;total&#39;:
print("{""}:{""}".format(key,a[key]))
f.close()
#退出通讯录
def exit():
exec("quit()")
#查找
def search(name):
f&#61;open(&#39;E:\Person.data&#39;,&#39;rb&#39;)
a&#61;pickle.load(f)
b&#61;0
for key in a.keys():
b&#43;&#61;1
if key&#61;&#61;name and b<&#61;a[&#39;total&#39;]&#43;1:
print("{}的号码是: {}".format(name,a[key]))
break
if b&#61;&#61;a[&#39;total&#39;]&#43;1 and key !&#61; name:
print("联系人不存在!")
break
f.close()
#删除
def deleate(name):
f&#61;open(&#39;E:\Person.data&#39;,&#39;rb&#39;)
a&#61;pickle.load(f)
f.close()
b&#61;0
for key in a.keys():
b&#43;&#61;1
if key&#61;&#61;name and b<&#61;a[&#39;total&#39;]&#43;1:
a.pop(name)
a[&#39;total&#39;]-&#61;1
f&#61;open(&#39;E:\Person.data&#39;,&#39;wb&#39;)
pickle.dump(a,f)
f.close()
print("删除成功!")
break
if b&#61;&#61;a[&#39;total&#39;]&#43;1 and key !&#61; name:
print("联系人不存在&#xff01;无法删除&#xff01;")
break
#修改
def change ():
x&#61;input("请输入所要修改联系人姓名:")
f&#61;open(&#39;E:\Person.data&#39;,&#39;rb&#39;)
a&#61;pickle.load(f)
f.close()
b&#61;0
for key in a.keys():
b&#43;&#61;1
if key&#61;&#61;x and b<&#61;a[&#39;total&#39;]&#43;1:
y&#61;input("请输入修后的号码:")
a[key]&#61;y
f&#61;open(&#39;E:\Person.data&#39;,&#39;wb&#39;)
pickle.dump(a,f)
f.close()
print("修改成功!")
break
if b&#61;&#61;a[&#39;total&#39;]&#43;1 and key !&#61; name:
print("联系人不存在&#xff01;")
break
#界面
def point ():
print("*******************************")
print("显示提示信息:*")
print("显示所有联系人:0")
print("查找联系人:1")
print("添加联系人:2")
print("删除联系人:3")
print("更改联系人资料:4")
print("退出通讯录:5")
print("*******************************")
#主程序
point()
while True:
x&#61;input("请输入您的选择:")
if x &#61;&#61; &#39;2&#39;:
add()
continue
if x&#61;&#61; &#39;0&#39;:
showall()
continue
if x&#61;&#61;&#39;5&#39;:
exit()
continue
if x&#61;&#61;&#39;1&#39;:
name&#61;input("请输入所要查找联系人的姓名:")
search(name)
continue
if x&#61;&#61;&#39;3&#39;:
name&#61;input("请输入所要删除联系人的姓名:")
deleate(name)
continue
if x&#61;&#61; &#39;4&#39;:
change()
continue
if x&#61;&#61;&#39;*&#39;:
point()
else:
print("输入选项不存在&#xff0c;请重新输入&#xff01;")
continue