需要注意的几点:
1.list的初始化
2.cPickle的load和dump方法的使用
3.class的存储
4.sort函数的使用
5.global标签的使用
6.初始的时候不存在memberlist.data或者文件内容为空解决方法
1 #Filename:MyAddressBook.py 2 import cPickle as p 3 import os 4 #Class Item 5 class Item: 6 def __init__(self,name,age,gender): 7 self.name = name 8 self.age = age 9 self.gender = gender 10 #the main menu of address book 11 def menu(): 12 '''the main menu of address book''' 13 print '' 14 print '1.Insert an item' 15 print '2.Delete an item' 16 print '3.Modify an item' 17 print '4.Display all items' 18 print '5.Sort all items' 19 print '6.Exit the program' 20 print 'What do you want to do?' 21 22 #initialization of system, load the member list 23 def begin(): 24 '''initialization of system, load the member list''' 25 global itemlist 26 if os.path.exists('memberlist.data') == True:#to judge whether the file exists 27 listfile = file('memberlist.data','r') 28 if len(listfile.read())!=0:#to judge whether the file is empty 29 itemlist = p.load(listfile) 30 listfile.close() 31 #exitance of system, store the menber list 32 def end(): 33 '''exitance of system, store the menber list''' 34 global itemlist 35 listfile = file('memberlist.data','w+') 36 p.dump(itemlist,listfile) 37 listfile.close() 38 #insert an item into the member list 39 def insert(): 40 '''insert an item into the member list''' 41 name = raw_input('Enter name:') 42 age = int(raw_input('Enter age:')) 43 gender = raw_input('Enter gender:') 44 item = Item(name,age,gender) 45 global itemlist 46 itemlist.append(item) 47 #print an item 48 def output(item): 49 '''print an item''' 50 print '%-15s%-5d%s'%(item.name,item.age,item.gender) 51 #print all items 52 def display(): 53 '''print all items''' 54 global itemlist 55 l = len(itemlist) 56 print 'name age gender' 57 for i in range(0,l): 58 output(itemlist[i]) 59 print '' 60 #delete an item by name from member list 61 def delete(): 62 '''delete an item by name from member list''' 63 name = raw_input('Enter the name you want to delete:') 64 global itemlist 65 l = len(itemlist) 66 for i in range(0,l): 67 if (itemlist[i].name == name): 68 itemlist.pop(i) 69 #update an item 70 def update(item): 71 '''update an item''' 72 item.name = raw_input('Enter name:') 73 item.age = int(raw_input('Enter age:')) 74 item.gender = raw_input('Enter gender:') 75 #update an item's information by name 76 def modify(): 77 '''update an item's information by name''' 78 name = raw_input('Enter the name you want to modify:') 79 global itemlist 80 l = len(itemlist) 81 for i in range(0,l): 82 if (itemlist[i].name == name): 83 update(itemlist[i]) 84 print 'Update done!' 85 #sort all items by name 86 def sort(): 87 global itemlist 88 itemlist.sort(None,key = lambda item:item.name) 89 #Here are the scripts 90 itemlist = [] #Notice here!!! 91 begin() 92 while True: 93 menu() 94 sel = int(raw_input()) 95 if sel == 1: 96 insert() 97 elif sel == 2: 98 delete() 99 elif sel == 3: 100 modify() 101 elif sel == 4: 102 display() 103 elif sel == 5: 104 sort() 105 else: 106 break 107 end() 108 print 'Good Bye!'