Python 實現簡單的通訊錄

簡單的通訊錄實現,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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章