用python写的命令行电话薄程序

最近读完了《A Byte of Python》,按照书上提示写了一个命令行电话薄程序(与书上要求相比略有缩减)。

程序比较糙,希望看到的大神能够不吝赐教……

以下是源代码

 1 #Filename: AddressBook.py
 2 
 3 import pickle
 4 
 5 class AddressBook:
 6     
 7     filename = ''
 8     
 9     def __init__(self, name, phone):    #initialize class
10         self.name = name
11         self.phone = phone
12         self.personlist = {name : phone}
13     def add(self):                      #add item
14         name = input('Enter a name: ')
15         if name in self.personlist.keys():
16             print('The name is already existing!')
17         else:
18             phone = input('Enter a phone number: ')
19             self.personlist[name] = phone
20             print('Saved!')
21     def modify(self):                   #modify item
22         name = input('Enter a name: ')
23         if name not in self.personlist.keys():
24             print('Failure! The name not here: ')
25         else:
26             phone = input('Enter a new phone: ')
27             self.personlist[name] = phone
28             print('Modify successfully!')
29     def delete(self):                   #delete item
30         name = input('Enter a name: ')
31         if name not in self.personlist.keys():
32             print('The name is not here.')
33         else:
34             self.personlist.__delitem__(name)
35             print('Delete successfully!')
36     def search(self):                   #search item
37         name = input('Enter a name: ')
38         if name not in self.personlist.keys():
39             print('The name is not here.')
40         else:
41             print('Name: {0}, Phone: {1}' .format(name, self.personlist[name]))
42     def show(self):                     #show items
43         for name in self.personlist.keys():
44             print('Name: {0}  Phone: {1}' .format(name, self.personlist[name]))
45     def dump(self):                     #dump data to disc
46         filename = self.name + '.data'
47         f = open(filename, 'wb')
48         pickle.dump(self.personlist, f)
49         f.close()
50     def load(self):                     #load data from disc
51         filename = self.name + '.data'
52         f = open(filename, 'rb')
53         self.personlist = pickle.load(f)
54         f.close()
55 if __name__ == '__main__':
56     
57     command = ['add','modify','search','delete','quit' ,'show']
58     person = AddressBook('hahaha', 123456789)
59 
60     ans = input("Do you want to load existing data from disc?(Y/N)")
61     if ans == 'Y':
62         person.load()
63 
64     while True:
65         str = input('What are you going to do(add/modify/search/delete/show/quit)?')
66 
67         if str in command:
68             if str == 'add':
69                 person.add()
70             elif str == 'modify':
71                 person.modify()
72             elif str == 'search':
73                 person.search()
74             elif str == 'delete':
75                 person.delete()
76             elif str == 'show':
77                 person.show()
78             else:
79                 answer = input("Your contacts list hasn't  been saved,save it now?(Y/N)")
80                 if answer == 'Y':
81                     person.dump()
82                     print('Dump successfully!')
83                 else:
84                     print ('Exit the System')
85                 break
86         else:
87             print ('Please input the command!')
88     

 

这个程序使用Wing IDE写的,不知道为什么不能写中文注释,有哪位知道原因能不能给说明下,先谢谢了。

 

 

posted @ 2013-04-12 23:02  liushaobo  阅读(340)  评论(0编辑  收藏  举报