这两天在学习python,主要参考简明教程http://www.swaroopch.com/byteofpython/ , 入门很不错,有兴趣推荐参考。
在16章中,作者建议读者写一个地址簿程序,还记得写它都是在大一学c语言的年代了,不过下午还是抽出了一点时间来写这个程序,写了两个版本,一个是使用python 字典来存储名字和地址,另一个版本则使用python的面向对象特性,来链式存储联系人对象。
版本一代码:
1import cPickle as p,sys,re
2
3# Author:nicky(nicky.jcoder@gmail.com)
4# July 24th,2008
5
6def readAdd():
7 f = file(ftxt)
8 map = p.load(f)
9 f.close()
10 return map
11
12def autoSave():
13 f=file(ftxt,'w')
14 p.dump(book,f)
15 f.close()
16
17def run():
18 while True:
19 try:
20 com = int(raw_input(welcome+features))
21 except:
22 print "\ninput error"
23 run()
24 if com == 1:
25 info = raw_input("input the name and address:")
26 infoList = re.split(' ',info)
27 add(infoList[0],infoList[1])
28 run()
29 autoSave()
30 book = readAdd()
31 elif com == 2:
32 info = raw_input("input the name:")
33 search(info)
34 run()
35 autoSave()
36 book = readAdd()
37 elif com == 3:
38 info = raw_input("input the name and address:")
39 infoList = re.split(' ',info)
40 modify(infoList[0],infoList[1])
41 run()
42 autoSave()
43 book = readAdd()
44 elif com == 4:
45 info = raw_input("input the name:")
46 delete(info)
47 run()
48 autoSave()
49 book = readAdd()
50 elif com ==5:
51 print 'All the people and their addresses are listed:'
52 browseAll()
53 elif com == 6:
54 autoSave()
55 sys.exit()
56 else:
57 print 'your input is invalid'
58 run()
59
60def add(people,address):
61 if people not in book:
62 book[people]=address
63 print '\nadd successfully'
64 else:
65 print '\nthe '+people+' exists in the address book'
66
67def search(people):
68 if people in book:
69 print "\n"+book[people]
70 else:
71 print '\nthe'+people+' is not in the address book'
72
73def modify(people,address):
74 if people in book:
75 book[people]=address
76 print '\nmodify successfully'
77 else:
78 print '\nthe'+people+' is not in the address book'
79
80def delete(people):
81 if people in book:
82 del book[people]
83 print '\ndelete successfully'
84 else:
85 print '\nsome errors has been arised'
86
87def browseAll():
88 for people,address in book.items():
89 print people+':'+address
90
91def judgeAddressbook():
92 f = file(ftxt,'w')
93 count =0
94 while True:
95 line=f.readline()
96 if len(line)==0:
97 break
98 count=count+1
99 if count == 0:
100 book={'test':'test'}
101 autoSave()
102
103if __name__ == '__main__':
104 welcome = '\nWelcome to use AddressBook made by nicky.jcoder@gmail.com'
105 features="\nyou can use the features listed:\n1:add people and their address information\n2:search\n3:modify\n4:delete\n5:Browse all the info in the addressbook\n6:exit the address book application\nInput your choice:"
106 ftxt = 'addressbook.txt'
107 book={}
108 judgeAddressbook()
109 book=readAdd()
110 run()
版本二代码:
1import cPickle as p,sys,re
2
3# Author:nicky(nicky.jcoder@gmail.com)
4# July 24th,2008
5
6def readAll():
7 f = file(ftxt)
8 list = p.load(f)
9 f.close()
10 return list
11
12def autoSave():
13 f=file(ftxt,'w')
14 p.dump(book,f)
15 f.close()
16
17class People:
18 def __init__(self,name,address):
19 self.name=name
20 self.address=address
21 def getName(self):
22 return self.name
23 def getAddress(self):
24 return self.address
25 def setAddress(address):
26 self.address=address
27
28def run():
29 while True:
30 try:
31 com = int(raw_input(welcome+features))
32 except:
33 print "\ninput error"
34 run()
35 if com == 1:
36 info = raw_input("input the name and address:")
37 infoList = re.split(' ',info)
38 add(infoList[0],infoList[1])
39 run()
40 autoSave()
41 book = readAdd()
42 elif com == 2:
43 info = raw_input("input the name:")
44 search(info)
45 run()
46 autoSave()
47 book = readAdd()
48 elif com == 3:
49 info = raw_input("input the name and address:")
50 infoList = re.split(' ',info)
51 modify(infoList[0],infoList[1])
52 run()
53 autoSave()
54 book = readAdd()
55 elif com == 4:
56 info = raw_input("input the name:")
57 delete(info)
58 run()
59 autoSave()
60 book = readAdd()
61 elif com ==5:
62 print 'All the people and their addresses are listed:'
63 browseAll()
64 elif com == 6:
65 autoSave()
66 sys.exit()
67 else:
68 print 'your input is invalid'
69 run()
70
71def add(people,address):
72 flag = -1
73 for item in book:
74 if people != item.getName():
75 instance = People(people,address)
76 book.append(instance)
77 print '\nadd successfully'
78 flag =0
79 if flag == -1:
80 print '\nthe '+people+' exists in the address book'
81
82def search(people):
83 flag=-1
84 for item in book:
85 if people == item.getName():
86 print "\n"+item.getAddress()
87 flag=0
88 if flag ==-1:
89 print '\nthe'+people+' is not in the address book'
90
91def modify(people,address):
92 flag=-1
93 for item in book:
94 if people == item.getName:
95 item.setAddress(address)
96 flag=0
97 print '\nmodify successfully'
98 if flag==-1:
99 print '\nthe'+people+' is not in the address book'
100
101
102def delete(people):
103 flag =-1
104 for item in book:
105 if people == item.getName:
106 del item
107 flag=0
108 print '\ndelete successfully'
109 if flag ==-1:
110 print '\nsome errors has been arised'
111
112
113def browseAll():
114 for item in book:
115 print item.getName()+":"+item.getAddress()
116
117def judgeAddressbook():
118 f = file(ftxt,'w')
119 count =0
120 while True:
121 line=f.readline()
122 if len(line)==0:
123 break
124 count=count+1
125 if count == 0:
126 instance=People("test1","test1")
127 book.append(instance)
128 autoSave()
129
130if __name__ == '__main__':
131 welcome = '\nWelcome to use AddressBook made by nicky.jcoder@gmail.com'
132 features="\nyou can use the features listed:\n1:add people and their address information\n2:search\n3:modify\n4:delete\n5:Browse all the info in the addressbook\n6:exit the address book application\nInput your choice:"
133 ftxt = 'addressbook_object.txt'
134 book=[]
135 judgeAddressbook()
136 book=readAll()
137 run()
运行方法应该不用说了
python 模块
效果图为
posted on 2008-07-24 17:51
wqwqwqwqwq 阅读(1549)
评论(3) 编辑 收藏 所属分类:
python