实现简单的通讯录
简单的通讯录实现,A byte of Python的例子
#!/bin/python #coding=utf8 import os try: import cPickle as p except: import pickle as p class Person: def __init__(self,name,mobile='',email='',address=''): self.name = name self.mobile = mobile self.email = email self.address = address def modifyInfo(self,mobile,email,address): self.mobile = mobile self.email = email self.address = address if os.path.exists("./contact.data")==False: dirlist = {} p.dump(dirlist,open("./contact.data",'w'),1) print "请输入要进行的操作(添加:a,删除:d,修改:m,查找:f,退出:q)" dict = p.load(open("./contact.data",'r')) flag = True while flag: choice = raw_input("请选择你的操作:a,d,m,f,q") if choice == 'a': n1 = raw_input("姓名:") m1 = raw_input("电话") e1 = raw_input("邮箱") a1 = raw_input("地址") per = Person(n1,m1,e1,a1) dict[n1]=per print '添加%s成功\n'%n1 elif choice == 'd': n2 = raw_input("姓名") del dict[n2] print '删除%s成功\n'%n2 elif choice == 'm': n3 = raw_input("姓名") m3 = raw_input("电话") e3 = raw_input("邮箱") a3 = raw_input("地址") per3 = dict[n3] per3.modifyInfo(m3,e3,a3) dict[n3] = per3 print '修改%s成功\n'%n3 elif choice == 'f': n4 = raw_input("姓名") try: per4 = dict[n4] print '%s 的信息如下'%n4 print '手机:%s,邮箱:%s,地址:%s'%(per4.mobile,per4.email,per4.address) except Exception,e: print e #打印异常信息
traceback.format_exc()
print '不存在这个人%s'%n4 elif choice == 'q': p.dump(dict,open("./contact.data",'w'),1) flag = False else: print '请输入正确的选项' continue