标签:
需要注意的几点:
1.list的初始化
2.cPickle的load和dump方法的使用
3.class的存储
4.sort函数的使用
5.global标签的使用
6.初始的时候不存在memberlist.data或者文件内容为空解决方法
2016.01.12
7.begin函数:初始的时候文件中有内容,调用read判断是否有内容后,需要调用seek移动指针到文件起始位置
8.delete函数:pop元素之后需要return或break,否则会出现指针越界错误
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 listfile.seek(0); 30 itemlist = p.load(listfile) 31 listfile.close() 32 #exitance of system, store the menber list 33 def end(): 34 ‘‘‘exitance of system, store the menber list‘‘‘ 35 global itemlist 36 listfile = file(‘memberlist.data‘,‘w+‘) 37 p.dump(itemlist,listfile) 38 listfile.close() 39 #insert an item into the member list 40 def insert(): 41 ‘‘‘insert an item into the member list‘‘‘ 42 name = raw_input(‘Enter name:‘) 43 age = int(raw_input(‘Enter age:‘)) 44 gender = raw_input(‘Enter gender:‘) 45 item = Item(name,age,gender) 46 global itemlist 47 itemlist.append(item) 48 #print an item 49 def output(item): 50 ‘‘‘print an item‘‘‘ 51 print ‘%-15s%-5d%s‘%(item.name,item.age,item.gender) 52 #print all items 53 def display(): 54 ‘‘‘print all items‘‘‘ 55 global itemlist 56 l = len(itemlist) 57 print ‘name age gender‘ 58 for i in range(0,l): 59 output(itemlist[i]) 60 print ‘‘ 61 #delete an item by name from member list 62 def delete(): 63 ‘‘‘delete an item by name from member list‘‘‘ 64 name = raw_input(‘Enter the name you want to delete:‘) 65 global itemlist 66 l = len(itemlist) 67 for i in range(0,l): 68 if (itemlist[i].name == name): 69 itemlist.pop(i) 70 break 71 #update an item 72 def update(item): 73 ‘‘‘update an item‘‘‘ 74 item.name = raw_input(‘Enter name:‘) 75 item.age = int(raw_input(‘Enter age:‘)) 76 item.gender = raw_input(‘Enter gender:‘) 77 #update an item‘s information by name 78 def modify(): 79 ‘‘‘update an item‘s information by name‘‘‘ 80 name = raw_input(‘Enter the name you want to modify:‘) 81 global itemlist 82 l = len(itemlist) 83 for i in range(0,l): 84 if (itemlist[i].name == name): 85 update(itemlist[i]) 86 print ‘Update done!‘ 87 #sort all items by name 88 def sort(): 89 global itemlist 90 itemlist.sort(None,key = lambda item:item.name) 91 print ‘Sort done!‘ 92 #Here are the scripts 93 itemlist = [] #Notice here!!! 94 begin() 95 while True: 96 menu() 97 sel = int(raw_input()) 98 if sel == 1: 99 insert() 100 elif sel == 2: 101 delete() 102 elif sel == 3: 103 modify() 104 elif sel == 4: 105 display() 106 elif sel == 5: 107 sort() 108 else: 109 break 110 end() 111 print ‘Good Bye!‘
http://www.cnblogs.com/CheeseZH/archive/2012/11/05/2754826.html
标签:
原文地址:http://www.cnblogs.com/tkinter/p/5629539.html