了解下python,看了觉得很简单,写一写发现还得练~
#!/usr/bin/python
import cPickle
import os
class ContactList:
'''this is a contact list'''
personList = {} #data container
def __loadData(self):
if (os.path.isfile('notebook') == False):
tmp = file('notebook','w')
tmp.close
return
f = file('notebook','r')
self.personList = cPickle.load(f)
f.close()
def __init__(self,person):
self.personList.append(person)
def __init__(self):
pass
def showList(self):
self.__loadData()
if (len(self.personList) == 0):
print 'No data'
return
for name,tel in self.personList.items():
print name+':'+tel
def addPerson(self,name,tel):
self.personList[name] = tel
self.save()
def delPerson(self,name):
del self.personList[name]
self.save()
def modifyTel(self,name,number):
self.personList[name] = tel
self.save()
def save(self):
f = file('notebook','w')
cPickle.dump(self.personList,f)
f.close()
#the list
notebook = ContactList()
while True:
userIn = raw_input('pls input your select: \ns(show) \na(add)\nd(delete)\nm(modify\ne(ecs)\ninput:')
if (userIn == 'a'):
name = raw_input('pls input name:')
tel = raw_input('pls input tel number:')
print 'Your input:person name %s tel number is %s' % (name,tel)
notebook.addPerson(name,tel)
notebook.save()
elif (userIn == 's'):
notebook.showList()
elif (userIn == 'e'):
print '-------------last list info-----------------'
notebook.showList()
print '-------------------------------------------'
break;
elif (userIn == 'd'):
name = raw_input('Input the name you want to delete:')
notebook.delPerson(name)
print 'current person list:'
notebook.showList()
elif (userIn == 'm'):
name = raw_input('Input the name want to update:')
tel = raw_input('Input the new tel for '+name+':')
notebook.modifyTel(name,tel)