Python 实现简单的通讯录

来源:互联网 发布:港股查询软件 编辑:程序博客网 时间:2024/05/22 06:33

简单的通讯录实现,A byte of Python的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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#打印异常信息<br>              <code class="functions">print</code><span> </span><code class="plain">traceback.format_exc()</code>
            print '不存在这个人%s'%n4
    elif choice== 'q':
        p.dump(dict,open("./contact.data",'w'),1)
        flag= False
    else:
        print '请输入正确的选项'
        continue
原创粉丝点击